-3

Write a function to check if an array contains a particular number.

Examples check([1, 2, 3, 4, 5], 3) ➞ true

My code (i know that it can be done WAY EASIER)

function check(arr, el) {
 let sum = 0
 for (i= 0; i<= arr.length; i++) {
  if (arr[i]==el) {
   sum++; }
  else  
 }
 if (sum == 1) {
  return true;}
  else false;
}

Can you help me understand why this code does not work please?

j08691
  • 204,283
  • 31
  • 260
  • 272
aerlins
  • 3
  • 1
  • 4
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Garrett Motzner Jun 04 '20 at 22:02
  • I spot a `}` right after `else` which is a syntax error. You can use your browser's development tools to see js errors. – lurker Jun 04 '20 at 22:04
  • Your posted code has syntax errors and cannot execute at all. Pease fix the code so we can understand what is wrong with its logic. – Gabriele Petrioli Jun 04 '20 at 22:07
  • More search for `if contains` and abandon your approach – Steve Jun 04 '20 at 22:20

4 Answers4

2

You have a built-in function for it - includes. Example usage:

[1,2,3,4].includes(3) // return true
[1,2,3,4].includes(8) // return false

Some of the other options:

  • some return true if the given condition return true for at least 1 of the array items, so you can do:

    arr.some(item => item === value)
    
  • At the low level you can do:

    for(let item of arr) if(item === value) return true;
    return false;
    
Yosef Tukachinsky
  • 5,570
  • 1
  • 13
  • 29
0

Your Code has an empty else clause.

function check(arr, el) {
    let sum = 0
    for (i= 0; i<= arr.length; i++) {
        if (arr[i]==el) {
            sum++; }
        else //<-- this else clause is empty and is causing you trouble 
    }
    if (sum == 1) {//This will only work if the element appears exactly once
        return true;}
        else false; //<-- and this won't return false as you intend.
}

Here is your code corrected.

function check(arr, el) {
let sum = 0
for (i= 0; i<= arr.length; i++) {
    if (arr[i]==el) {
        sum++; 
    }
}
if (sum >= 1) {
    return true;
}
return false;
}

And here is my example

function check(arr, el) {
    for(let i = 0; i < arr.length;i++){
        if(arr[i] === el){
            return true;
        }
    }
    return false;
}

Hope it is useful

O.Cuenca
  • 121
  • 1
  • 5
0

A couple of things.

  1. Add a semicolon at the end of line 2. (This isn't necessarily necessary but prevents possible errors.)

  2. Remove the equals from i<= arr.length in line 3 to assure that your not excessing an index of the array that doesn't exist.

  3. You don't need the else in line 6. You have no code to run in the else statement so it isn't necessary.

  4. At line 10, you have an else statement missing it's curling brackets. You need these around the code block you're running for the else statement. Also, you're missing the word return.

I've made a correct one here:

function check(arr, el) {
 let sum = 0;
 for (i = 0; i < arr.length; i++) {
  if (arr[i]==el) {
      sum++; 
        }  
 }
 if (sum == 1) {
        return true;
    } else {
        return false;
    }
}
johnjuly
  • 1
  • 2
0

// Correction:
function check(array, element) {
  let itContains = false // Initialized to be boolean.

  for (let i = 0; i < array.length; i++) { // If you start with 0 you use '<' not '<='. Just count :)
    if (array[i] === element) { // Use '===' if you want to check if it is of the same type and value.
      itContains = true
      break // You do not need to go through the whole loop. You break it when you find a positive match.
    } // You do not need 'else' here because you initialized 'itContains' to be false.
  }

  return itContains // You just return proper boolean - false or true.
}

console.log(check([1, 2, 3, 4], 2)) // Returns 'true'.
console.log(check([1, 2, 3, 4], 5)) // Returns 'false'.

// Elegant solution:
function checkElegant(array, element) {
  return array.includes(element) // 'includes' method returns true or false.
}

console.log(checkElegant([1, 2, 3, 4], 2)) // Returns 'true'.
console.log(checkElegant([1, 2, 3, 4], 5)) // Returns 'false'.
Slobodan
  • 1
  • 1