c-mcbride

variableNameify.js

Jan 29th, 2024
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Write a function variableNameify(words) that takes in an array of words. The function should return a string representing the //variable name obtained by combining the words and captitalizing them in mixCased style.
  2.  
  3. function variableNameify(words){
  4.     let newWordArray  = [];
  5.     newWordArray.push(words[0].toLowerCase())
  6.    
  7.     for(let i = 1; i < words.length; i++){
  8.         let currentWord = words[i];
  9.         currentWord = currentWord.toLowerCase();
  10.  
  11.         let firstLetter = currentWord.charAt(0);
  12.         let firstLetterCap = firstLetter.toUpperCase();
  13.  
  14.         let remainingLetters = currentWord.slice(1);
  15.         let capitalizedWord = firstLetterCap + remainingLetters;
  16.  
  17.         newWordArray.push(capitalizedWord);
  18.     }
  19.  
  20.     newWordArray = newWordArray.join("");
  21.     return newWordArray;
  22. }
  23.  
  24. console.log(variableNameify(['is', 'prime'])) // => 'isPrime'
  25. console.log(variableNameify(['remove', 'last', 'vowel'])) // => 'removeLastVowel'
Advertisement
Add Comment
Please, Sign In to add comment