0

Both arrays contain the string 'hello', but only the first logs true. Why?

function containsHello(array){
  for(let i = 0; i < array.length; i++){
    if(array[i] === 'hello'){
      return true;
    } else {
      return false;
    }
  }
}

console.log(containsHello(['hello', 'no', 'yes'])); // logs true
console.log(containsHello(['no', 'hello', 'yes'])); // logs false

2
  • Put a console.log in the loop. How many times does it print? How many times do you expect it to print? Commented Oct 17, 2019 at 19:42
  • Try == instead of ===, see what happens. Commented Oct 17, 2019 at 19:42

9 Answers 9

2

In your code where you are returning false, you are returning too soon (before checking all elements)

you should return false after the loop is finished

function containsHello(array){
  for(let i = 0; i < array.length; i++){
    if(array[i] === 'hello'){
      return true;
    }
  }
  return false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Basically, if the element is NOT 'hello', it immediately returns false but hasn't got a chance to check any potentially preceding elements. Remember if a function reaches a return statement it (prematurely) ends. Just a small logic mistake.

Also...

includes is a method. I don't even need to explain.

var someList = ['a', 'b', 'c'];
console.log(someList.includes('c')) // --> true

1 Comment

I didn't know how to use .includes() until i posted this, so thank you for that explanation.
0

Thats because for the second the hello is at index position 1. While iterating over the array, it sees the 'no' value first and just returns.

Comments

0

The problem with your code is that it will always return on the first item, and never check the other values. Remove the else block, and return false after the loop.

Alternatively, just use array.includes().

Comments

0

You are returning false if the first element is not equal to 'hello'
Instead try this function

function containsHello(array){
  for(let i = 0; i < array.length; i++){
    if(array[i] === 'hello'){
      return true;
    }
  }
  return false
}

Comments

0

There is a logical error in your function. It returns false if the first element is not 'hello'. However, this does not make sense, as 'hello' could still be contained in the other values. This can be fixed by returning false only after checking all of the values.

function containsHello(array){
  for(let i = 0; i < array.length; i++){
    if(array[i] === 'hello'){
      return true;
    }
  }
  return false;
}

console.log(containsHello(['hello', 'no', 'yes']));
console.log(containsHello(['no', 'hello', 'yes']));
console.log(containsHello(['no','yes','also no']));

Comments

0

It could be better this:

function containsHello(array){
  return array.indexOf('hello') !== -1; 
}

1 Comment

Index checks for potentially non-existent elements are ugly when Array.includes exists.
0

You could use .includes() if you want to skip the for loop.

function containsHello(array){
  if(array.includes("Hello")) {
     return true;
  } else {
    return false;
  }
}

or you could do the following if you want to search for different words

function containsWord(word, array){
      if(array.includes(word)) {
         return true;
      } else {
        return false;
      }
    }

3 Comments

At that point, it's just a wrapper for includes.
I agree, I just gave it to him in a function because his initial post had a function. Otherwise a console.log(array.contains('x')); works perfectly fine. Ah, I see you posted a solution with .includes also.
I like this explanation as well, others have mentioned .includes() but i just wanted to see it as a function.
0

You can use the .includes() function for this purpose like this:

[].includes('value')

Then:

console.log(['no','hello','yes'].includes('hello')); //logs true
console.log(['hello','no','yes'].includes('hello')); //logs true

2 Comments

You don't really explain the initial problem, which is pretty important.
I know, I just tried to answer the initial question "How to see if an array has a value and return true if it does javascript". I will consider it for next time, thanks for your remark.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.