user12076510
user12076510

Reputation:

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?

Upvotes: 4

Views: 1799

Answers (10)

Satyanshu Gupta
Satyanshu Gupta

Reputation: 11

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

Upvotes: 1

Jose Luis
Jose Luis

Reputation: 11

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

}

Upvotes: 1

Tuseef ashraf
Tuseef ashraf

Reputation: 1

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!!

Upvotes: 0

Maninder
Maninder

Reputation: 1919

package ArrayPractice;

import java.util.Scanner;

public class Question4 {
    public String sevenBoom(int[] test) {
        for (int i = 0; i < test.length; i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(test[i]);
            String temp = sb.toString();
            if (temp.contains("7")) {
                return "Boom";
            }
        }
        return "None of the items contain 7 within them.";
    }

    public static void main(String[] args) {
        System.out.println("Enter the array Range: ");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int[] testing = new int[i];
        for (int x = 0; x < testing.length; x++) {
            System.out.println("Enter the array number: " + x);
            testing[x] = sc.nextInt();
        }
        Question4 question3 = new Question4();
        System.out.println(question3.sevenBoom(testing));
    }
}

Upvotes: -1

Maninder
Maninder

Reputation: 1919

package ArrayPractice;
import java.util.Scanner;
public class Question3 {
    public String sevenBoom(int [] test) {
        for(int i=0;i<test.length;i++){
            if(test[i]==7){
                return "Boom";}
            if(test[i]>=10){
                StringBuilder sb= new StringBuilder();
                sb.append(test[i]);
                String temp= sb.toString();
                for(int v=0;v<temp.length();v++){
                    char c= temp.charAt(v);
                    if(c=='7'){
                        return "Boom";
                    }}}
        }return "None of the items contain 7 within them.";}
    public static void main(String[] args) {
        System.out.println("Enter the array Range: ");
        Scanner sc= new Scanner(System.in);
        int i= sc.nextInt();
        int [] testing= new int[i];
        for(int x=0;x<testing.length;x++){
            System.out.println("Enter the array number: "+x);
            testing[x]= sc.nextInt();}
        Question3 question3= new Question3();
        System.out.println(question3.sevenBoom(testing));
    }}

Upvotes: -1

Jj_Ebeling
Jj_Ebeling

Reputation: 41

One year later, but try this one!

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

Upvotes: 1

Carsten Massmann
Carsten Massmann

Reputation: 28196

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

Upvotes: 0

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

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

Upvotes: 0

Dalorzo
Dalorzo

Reputation: 20014

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

Upvotes: 2

Alex Vovchuk
Alex Vovchuk

Reputation: 2926

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

Upvotes: 2

Related Questions