Home > Mobile >  How to break when iteration is supposed to end after "continue"?
How to break when iteration is supposed to end after "continue"?

Time:12-24

So I am doing some challenges on codewars, and got stuck at the following problem:

  • Write a function that checks if a string contains all letters of the alphabet, either in upper- or in lowercase.

Here is my code so far:

function isPangram(string) {
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26; i  ) {
    if (string.toLowerCase().includes(alphabet[i])) {
      continue;
    } else {
      return false
    }
    return true
  }
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))

However, "continue" does not work as I had in mind, since at the point of 'alphabet[26]', it will go to the else-block and return false. I obviously cannot return true instead of continue, because then it will always return true as soon as the string contains any letter. Another thing I was thinking about is if you even need the for-loop. Logically I wouldn't need it if I could just write

if (string.toLowerCase().includes(alphabet[0-25])){" 

but that does not work either.

CodePudding user response:

This is a bit simpler:

const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

function isPangram(input_string) {
  let string = input_string.toLowerCase()
  for (let i = 0; i < 26; i  ) {
    if (!string.includes(alphabet[i])) {
      return false
    }
  }
  return true
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))

No need for a continue when a check for "not true" will do, and your return true statement was inside your for loop, which would lead to a isPangram("a") evaluating to true.

CodePudding user response:

Other answers and comments showed you your error.

Here is a shorter version using every

let alphabet = [..."abcdefghijklmnopqrstuvwxyz"]; // spread makes an array

const isPangram = string => {
  const str = string.toLowerCase();
  return alphabet.every(letter => str.includes(letter)); // every returns as soon as something is false
};

console.log(isPangram("The quick brown fox jumps over the lazy dog"))
console.log(isPangram("The quick brown foe jumps over the lazy dog"))

Using a Set

const isPangram = string =>  {
  const chars = [...string.toLowerCase()]; // Convert to an array of lowercase letters
  const letters = new Set(); // a set stores unique values
  chars.forEach(char => (char >= 'a' && char <= 'z') && letters.add(char));
  return letters.size === 26; // number of letters in the English alphabet
};

console.log(isPangram('The quick brown fox jumps over the lazy dog')); // true
console.log(isPangram('This is not a pangram')); // false
console.log(isPangram('ABCDEFGHIJKLMNOPQRSTUVWXYZ')); // true

CodePudding user response:

I think maybe I'd simplify it slightly... You don't need the continue statement if you use the "!" to invert the boolean returned by the includes test.

function isPangram(string) {
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26; i  ) {
    if (!string.toLowerCase().includes(alphabet[i])) {      
      return false
    }
  }
  return true
}

console.log(isPangram("The quick brown fox jumps over the lazy dog"))

CodePudding user response:

Just did a little change, worked for the example you provided.

function isPangram(string) {
    let response = true;
  let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

  for (let i = 0; i < 26;) {
    if (string.toLowerCase().includes(alphabet[i])) {
      i  ;
    } else {
      response = false
      break;
    }
  }
  return response;
}

console.log(isPangram("The quick brown fox jumps over the lazzy dog"))
console.log(isPangram("The quick brown fox jumps over the lazzy d"))

  • Related