h.laks
h.laks

Reputation: 11

Difficulty with Boolean and arrays in Javascript

Here, I am trying to write a code that takes a list of integers as a parameter and searches for the value 7. The function will return a boolean value representing true if 7 is in the list and false if it is not. This is what I have tried so far:

enter image description here

Could it be something with my else statement? Do I end it too soon? Or not soon enough?

Upvotes: 0

Views: 609

Answers (5)

oezguensi
oezguensi

Reputation: 950

Your declaration of the if statement is wrong. The else tag is on the wrong line. If you use else, it should come after the if-block.

But moving the else-block up, won't fix your function, because it will only return true if the first element in your array is a 7.

There are many good ways to do it, such as using higher order functions, but as it seems you are new to the language.

EDIT: Therefore, I would suggest one of the two simple ways below:

1) You could store the number of 7s found in the array in a for loop. Afterwards return false if the number of 7s is 0 or true if it the number is higher than 0

2) Another, much quicker way would be to return true in the for-loop when you encounter a 7 and after the for-loop just return false. Because a return statement exits the scope - which is your function - not only the for-loop would come to an end but also your function would return true much earlier.

For the second option, your function would look like this:

function find_value(list) {
  for (let i = 0; i < list.length; i++) {
    if(list[i] == 7) {
      return true
    }
  }
  return false
}

Upvotes: 2

Sachin Shah
Sachin Shah

Reputation: 4533

Try this way

function search(list , value){      // Value = 7; 
    return true ? list.indexOf(value) > -1 : false
}

Proof

Upvotes: 0

Matt Way
Matt Way

Reputation: 33209

Javascript already has a function to do this. Array.prototype.includes(). You use it like this:

const containsSeven = list.includes(7)

If you are looking for something more complicated, like whether an item is an object and contains a particular key value pair for example, you can use Array.prototype.some()

Upvotes: 3

Yosvel Quintero
Yosvel Quintero

Reputation: 19090

You can coerces to boolean the result of Array.prototype.find() which returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

Code:

const list1 = [4, 5, 6, 7, 8];
const list2 = [1, 2, 3, 4, 5];
const findTheNumber = (arr, num) => !!arr.find(n => n === num);

console.log(findTheNumber(list1, 7));
console.log(findTheNumber(list2, 7));

Upvotes: 2

Gauravsa
Gauravsa

Reputation: 6528

You can simply use an array and use includes as per ECMA2016 like below:

if([2,5,7].includes(value)) {
    return true;
}
return false;

or with list

var flag = false;

for(var i=0; i<arguments.length; i++)
{   if(arguments[i] == this) { flag = true; break; }}

return flag;

Upvotes: 3

Related Questions