4

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?

6
  • 9
    correct ... arr.join().includes(7) will do what you want without pesky iteration Commented Oct 2, 2019 at 4:01
  • 2
    35, 4, 9, 37 there is no 7 Commented Oct 2, 2019 at 4:02
  • @joyBlanks yes, in the 37 Commented Oct 2, 2019 at 4:05
  • 1
    includes will get you an exact match of the items not the digits/chars of the item. you already have a solution in comment from JaromandaX Commented Oct 2, 2019 at 4:06
  • You just need to find if a number contains "7" right? Commented Oct 2, 2019 at 9:04

8 Answers 8

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

Here is one way using Regex in combination Array.prototype.join to match only 7 number:

[35, 4, 9, 37,7].join().match(/\b7\b/) !== null

This searches for only 7 within your array joined

/\b7\b/

Then all it is required is:

function sevenBoom(arr) {
       var has7 = arr.join().match(/\b7\b/) !== null;
       if (has7) { 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!"));

Comments

1

One year later, but try this one!

const sevenBoom = arr => /7/.test(arr) ? 'Boom!' : 'there is no 7 in the array';

1 Comment

Doesn't work, matches 76 and 37
1

private static void boom(int[] numbers){

    String boom = "Boom!";
    String noseven ="there is no 7 in the array";
    String string_n;
    char[] chArray = new char[30] ;
    
    int numLenth = numbers.length;
    int y=0;
                    
    for (int j = 0 ; j < numLenth; j++)
        
    {
        
        string_n = String.valueOf(numbers[j]);
        for (int i = 0 ; i < string_n.length(); i++)
            
            
        {
            chArray[i] = string_n.charAt(i);
            
            
            
        if (chArray[i] == '7' )
            y = 1 ;
            
        }
        
        }
    
    
    

    if (y == 1)
        System.out.println(boom);
    else
        System.out.println(noseven);
    
}

public static void main(String[] args) {

    int[] arrayofnum1 = {1,2,0,4,5,6,9,8,9};
    int[] arrayofnum2 = {7,17,27,5,6,3,12354,1234578};
    int[] arrayofnum3 = {12345689,6532198,65632198};
    
    
    boom(arrayofnum1);
    boom(arrayofnum2);
    boom(arrayofnum3);

}

Comments

1

function sevenBoom(arr) {
    let regex = /7/g;
    if(arr.toString().match(regex)){
        return "Boom!";
    }else{
        return "there is no 7 in the array";
    }
}
console.log(sevenBoom([2, 6, 7, 9, 3]))
console.log(sevenBoom([33, 68, 400, 5]))
console.log(sevenBoom([86, 48, 100, 66]))
console.log(sevenBoom([76, 55, 44, 32]))
console.log(sevenBoom([35, 4, 9, 37]))

function sevenBoom(arr) {  
    let regex = /7/g;
    if(arr.toString().match(regex)){
        return "Boom!";
    }else{
        return "there is no 7 in the array";
    }
}
console.log(sevenBoom([2, 6, 7, 9, 3]));
console.log(sevenBoom([33, 68, 400, 5]));
console.log(sevenBoom([86, 48, 100, 66]));
console.log(sevenBoom([76, 55, 44, 32]));
console.log(sevenBoom([35, 4, 9, 37]));

Comments

0

We can also do this using Array.prototype.some which will return true immediately once the number in the array includes 7:

function sevenBoom(arr) {
   if (arr.some(num => `${num}`.includes('7'))) {
      return "Boom!"
   } 
  return "there is no 7 in the array"
}

console.log(sevenBoom([2, 6, 7, 9, 3]))
console.log(sevenBoom([33, 68, 400, 5]))
console.log(sevenBoom([86, 48, 100, 66]))
console.log(sevenBoom([76, 55, 44, 32]))
console.log(sevenBoom([35, 4, 9, 37]))

1 Comment

num => ${num}.includes('7') should be num => num == 7, OP doesn't want it to match 37 and 76
0

Or, taken to the extreme in ES6 notation. "spot the value 7":

[[2, 6, 7, 9, 3],[33, 68, 400, 5],[86, 48, 100, 66],[76, 55, 44, 32],[35, 7, 9, 37]]
.forEach(arr=>console.log(arr.toString(),arr.some(el=>el==7)?'boom, value==7':'nope'));

Just realized the number 7 is to be spotted as a digit and not the total number. "spot the digit 7":

[[2, 6, 7, 9, 3],[33, 68, 400, 5],[86, 48, 100, 66],[76, 55, 44, 32],[35, 7, 9, 37]]
.forEach(arr=>console.log(arr.toString(),arr.toString().match(/7/)?'boom, digit 7 found':'nope'));

Comments

0

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

const sevenBoom = (arr) => {
  let statement = "there is no 7 in the array";
  arr.forEach((element) => {
    element = element.toString();
    if (element.length === 1) {
      if (element == 7) {
        statement = "Boom!";
      }
    } else if (element.length > 1) {
      element = element.split("");
      // console.log(element);
      for (let i = 0; i < element.length; i++) {
        // console.log(typeof element[i]);
        if (element[i] == 7) {
          // console.log(typeof element[i]);
          statement = "Boom!";
        }
      }
    }
  });
  return statement;
};

// console.log(sevenBoom([1, 2, 3, 4, 5, 6, 7]));
// console.log(sevenBoom([5, 25, 77]));
// console.log(sevenBoom([1, 2, 4]));
// console.log(sevenBoom([42, 76, 55, 44, 32]));
// console.log(sevenBoom([2, 55, 60, 97, 86]));

Simple code using array's functions and loops please let me know if this was helpful to you!!

Comments

Your Answer

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