Answers for "capitalize"

0

Alternate capitalization

const capitalize = s => {
  s = s.split('')
  let finalArr = []
  let i = 0
  while(i < 2){
    let str = ''
    s.forEach((letter, index) => {
      if(i % 2 === 0 && index % 2 === 0) str += letter.toUpperCase()
      else if(i % 2 !== 0 && index % 2 !== 0) str += letter.toUpperCase()
      else str += letter
    })
      
    finalArr.push(str)
    i++
  }
  return finalArr
};
console.log(capitalize("abcdef"))
Posted by: Guest on June-24-2020
-1

capitalize

function capitalize1(str) {
  const arrOfWords = str.split(" ");
  const arrOfWordsCased = [];

  for (let i = 0; i < arrOfWords.length; i++) {
    const word = arrOfWords[i];
    arrOfWordsCased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
  }

  return arrOfWordsCased.join(" ");
}
Posted by: Guest on August-17-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language