Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding if number exists in array

Create a function that takes an array of numbers and return "Boom!" if the number 7 appears in the array. Otherwise, return "there is no 7 in the array".

function sevenBoom(arr) {

   if (arr.includes(7)) {

      return "Boom!"

   } 

  return "there is no 7 in the array"

}

TESTS

Test.assertEquals(sevenBoom([2, 6, 7, 9, 3]), "Boom!")
Test.assertEquals(sevenBoom([33, 68, 400, 5]), "there is no 7 in the array")
Test.assertEquals(sevenBoom([86, 48, 100, 66]), "there is no 7 in the array")
Test.assertEquals(sevenBoom([76, 55, 44, 32]), "Boom!")
Test.assertEquals(sevenBoom([35, 4, 9, 37]), "Boom!")

The last 2 tests are failing, im assuming that is the case because it's looking for a 7, not just having a 7 in the number itself.

How could I correct this?

NOT A DUPLICATE

This has nothing to do with substrings or strings. Why do people like marking things as duplicate so much?


1 Answers

Solution without regular expressions:

function sevenBoom(arr) {
    for(let el of arr) {
        if(el.toString().split('').includes('7')) {
            return "Boom!"
        }
    }
    return "there is no 7 in the array"
}

console.log(sevenBoom([2, 6, 7, 9, 3], "Boom!"))
console.log(sevenBoom([33, 68, 400, 5], "there is no 7 in the array"))
console.log(sevenBoom([86, 48, 100, 66], "there is no 7 in the array"))
console.log(sevenBoom([76, 55, 44, 32], "Boom!"))
console.log(sevenBoom([35, 4, 9, 37], "Boom!"));
like image 68
Alex Vovchuk Avatar answered Nov 04 '25 06:11

Alex Vovchuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!