Mao
Mao

Reputation: 3

comparing elements in two arrays javascript

I've just started learning javascript and I'm trying to understand loops. I have this simple function that is supposed to compare elements in between a word and an array of vowels.

I'm trying to loop over the vowels and then loop over the word and then, with a if/else statement just console.log the letter of the word that is NOT a vowel.

when I use " !== " it just returns all the characters in the word but when I use "===" it returns just the vowels.

I am aware that this can be done in other ways but right now I need to understand why this happens.

Example :

function checkvowel("hello") {

      const vowels = ["a","e","i","o","u"];

      for(let i = 0; i<vowels.length; i++){

        for(let j=0; j<word.length; j++){

          if (word[j] === vowels[i]){
             console.log(word[j])  --> OUTPUT "e" "o"
          }
          
         But when I do :

         if (word[j] !== vowels[i]){
             console.log(word[j])  --> OUTPUT "h" "e" "l" ...
          }
    
        }
      }

Why does this happens?

Thank you

Upvotes: 0

Views: 594

Answers (5)

DCR
DCR

Reputation: 15665

you need to THINK about what's going on in your loops!

do you want to say for each vowel check letters in word or for each letter in word check vowels. Clearly, the latter.

step 1:switch loops
step 2: create check flag

function checkvowel(word) {
      
  const vowels = ["a","e","i","o","u"];

  for(let i = 0; i<word.length; i++){
    var check = false;
    for(let j=0; j<vowels.length; j++){
      if (word[i] == vowels[j])check = true;       
    }
     if (!check) console.log('not a vowel ',word[i])
     if (check) console.log('    a vowel ',word[i])
  }
}
checkvowel('hello')

Upvotes: 0

Pranav Rustagi
Pranav Rustagi

Reputation: 2731

Well there are many better ways to implement the same, but since you asked the explanation of why it's generating result like that, here is the reason :

For the if condition with !== operator, if condition evaluates to true for all the letters of word which are not equal to the ith element of vowel array.

For i = 0 : Output is h e l l o. That is because none of the letters in the word is equal to the 0th element , i.e. "a".

For i = 1 : Output is h l l o. This is because all the letters of word except "e" are not equal to 1st element, i.e. "e".

The consoles are generated for other iterations in similar manner.

Upvotes: 0

0stone0
0stone0

Reputation: 43934

First of all;

function checkvowel("hello") {

Should include the name of the parameter like so:

function checkvowel(word = "hello") {

That said, you're looping though the vowels, and checking each index of the world, logging it if it doesn't match.

If you're trying to find the letters that arn't vowels, you'll need to check if any of those letters exist in the vowel array, not just index checking;

For example, use includes like so;

function checkvowel(word = "hello") {

    const vowels = ["a", "e", "i", "o", "u"];

    // For each letter in word
    for (let j = 0; j < word.length; j++) {
        
        // If this letter does not exist in vowels
        if (!vowels.includes(word[j])) {

            // Log it
            console.log(word[j]);
        }
    }
}
checkvowel();

Upvotes: 1

symlink
symlink

Reputation: 12209

Since you are looping through both the word and the vowels, whenever the current vowel doesn't match the current letter of the word, you are getting a false negative. For example, the "e" in "hello" does not match "a","i","o", or "u", so even though it is a vowel, it is logged as a consonant those four times. To fix this, just loop through the letters in the word, and use Array.includes() to see if the current letter is included in the array of vowels:

function checkvowel(word) {
  const vowels = ["a", "e", "i", "o", "u"]
  for (let i = 0; i < word.length; i++) {
    if (vowels.includes(word[i])) {
      console.log("vowel: ", word[i])
    }
    if (!vowels.includes(word[i])) {
      console.log("consonant: ", word[i])
    }
  }
}

checkvowel("hello")

Upvotes: 1

islam hany
islam hany

Reputation: 148

When you do the following

if (word[j] === vowels[i]){
             console.log(word[j])  --> OUTPUT "e" "o"
}

it means that when the character of the word array at position j equals the character of vowels array at position i print that character . Which in this case is a vowel.

But when you do

if (word[j] !== vowels[i]){
             console.log(word[j])  --> OUTPUT "h" "e" "l" ...
}

it means that when the character of the word array at position j does not equal the character of vowels array at position i print that character . Which in this case is any character that is not a vowel.

Upvotes: 0

Related Questions