0

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?

4
  • 1
    Post your code as plain text, not an image. See formatting for code formatting help. Commented Nov 6, 2018 at 3:00
  • Get rid of the else and it will work fine. The construct is called if-else, not for-else. Commented Nov 6, 2018 at 3:01
  • This is not Python, you can't have an else clause on for. It can only be used with if. Commented Nov 6, 2018 at 3:02
  • I realize this is probably a coding exercise, but if you favor brevity, you could do something as simple as function find_value(list) { return list.includes(7); } Commented Nov 6, 2018 at 3:02

5 Answers 5

3

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;
Sign up to request clarification or add additional context in comments.

Comments

3

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()

3 Comments

Any reason to use some and write your own comparison instead of just using includes? Genuinely curious.
You're right. Includes is much better. Slipped my mind from the array prototype list. Thanks.
In this case, I think includes is much better. I think I went to some() naturally because I am always checking for something more complex. Again, thanks for pointing this out.
2

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
}

2 Comments

Although it's a learning experience, I very much disagree with your proposed solution. If you had an array of 5,000 items and the first item was 7, your solution will continue evaluating the other 4,999 items.
Yes you are correct. A quicker way would be to return true directly when a 7 is encountered, but I wanted to keep it simple.
2

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));

Comments

0

Try this way

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

Proof

7 Comments

Again... why are you using a ternary operator? You are quite literally saying "If true is true...". Also indexOf is case-sensitive. return list.indexOf(value) > -1 would work just fine.
@TylerRoper It is the simple one if list has index of value then return true otherwise return false.
@TylerRoper Can you please tell me , what's wrong in my answer.
I literally just did...? indexOf is case-sensitive. I'm still very confused about the ternary operator but I've downvoted it because it simply doesn't work.
The code in the image that you've attached is not the same as the code in your answer though. It's index Of , not index of . I also don't know how else to explain the ternary operator thing. Would you ever write if (true) { ... } else { ... }? Because that is literally what true ? ... : ... is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.