mercredi 6 octobre 2021

What is possibly wrong with the second function here?

I am learning array function and came across this quiz. To get all caps from a string , first function what is possibly wrong with the second function here? Trying to find solution to this code. But could not, with the break, it works, but not with continue, what is possibly wrong with the second function here?

function getCapsWords(str) {
  let result = []; // Empty array
  const words = str.split(" "); // array
  let i = 0;
  while (i < words.length) {
    let word = words[i];
    word = word[0].toUpperCase() + word.slice(1);
    if (word === "Good") break;
    result.push(word); // populate arr
    i++;
  }
  return result.join(" ");
}

function getCapsWords(str) {
  let result = []; // Empty array
  const words = str.split(" "); // array
  let i = 0;
  while (i < words.length) {
    let word = words[i];
    word = word[0].toUpperCase() + word.slice(1);
    if (word === "Good") continue;
    result.push(word); // populate arr
    i++;
  }
  return result.join(" ");
}

getCapsWords("Hello World Good Good Morning!!!");

Aucun commentaire:

Enregistrer un commentaire