简体   繁体   English

查找数组中是否存在数字

[英]Finding if number exists in array

Create a function that takes an array of numbers and return "Boom."创建一个接受数字数组并返回“Boom”的 function。 if the number 7 appears in the array, Otherwise.如果数字 7 出现在数组中,否则。 return "there is no 7 in the array".返回“数组中没有 7”。

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.最后 2 次测试失败,我假设是这种情况,因为它正在寻找7 ,而不仅仅是数字本身有7

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?为什么人们如此喜欢将事物标记为重复?

Here is one way using Regex in combination Array.prototype.join to match only 7 number:这是使用 Regex 组合Array.prototype.join仅匹配 7 个数字的一种方法:

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

This searches for only 7 within your array joined这仅在您的阵列中搜索 7 个加入

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

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

One year later, but try this one!一年后,但试试这个!

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

We can also do this using Array.prototype.some which will return true immediately once the number in the array includes 7 :我们也可以使用Array.prototype.some来做到这一点,一旦数组中的数字包括7 ,它将立即返回true

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

Or, taken to the extreme in ES6 notation.或者,在 ES6 表示法中发挥到极致。 "spot the value 7": “发现值 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.刚刚意识到数字 7 是一个数字而不是总数。 "spot the digit 7": “找出数字 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'));

Create a function that takes an array of numbers and return "Boom."创建一个接受数字数组并返回“Boom”的 function。 if the digit 7 appears in the array, Otherwise.如果数字 7 出现在数组中,否则。 return "there is no 7 in the array".返回“数组中没有 7”。

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

private static void boom(int[] numbers){私人 static 无效繁荣(int [] 数字){

    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) {公共 static 无效主(字符串 [] 参数){

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

} }

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

Array.prototype.some() is the only correct way of doing this. Array.prototype.some()是唯一正确的方法。

 const sevenBoom = arr => arr.some(n => n == 7)? "Boom:". "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]))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM