简体   繁体   English

输入是否包含 0-9 JS 的所有数字

[英]Is input contains all numbers from 0-9 JS

Hey guys im struggling with this exercise.嘿,伙计们,我正在为这个练习而苦苦挣扎。 Im trying to check is input contains all numbers from 0-9.我试图检查输入是否包含 0-9 的所有数字。 For example if input is "0a1b2345asd6s7e89" it should return true, "123789" it should be false.例如,如果输入是“0a1b2345asd6s7e89”它应该返回真,“123789”它应该是假的。 I tryed code below but i got feeling that im heading in wrong direction.我尝试了下面的代码,但我觉得我正朝着错误的方向前进。 Maybe regexp?也许正则表达式? Please help.请帮忙。

 const test = (input) => { const arrayOfNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const inputArray = [...input] inputArray.every(el => { arrayOfNumbers.forEach(elNumber => { elNumber === el }) }) }

const test = (a, b) => a.every(el => b.includes(el) ? true : false)

Then you can use it like然后你可以像这样使用它

const ax = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var ab = "0a1b2345asd6s7e8"
test(ax,ab)
// -> false
ab = "0a1b2345asd6s7e89"
test(ax,ab)
// -> true

Try every and includes :尝试everyincludes

 const test = input => [...Array(10).keys()].every(digit => input.includes(digit)); console.log("Expected output - true:", test("0a1b2345asd6s7e89")); console.log("Expected output - false:", test("123789"));

[...Array(10).keys()] is an array of 0 to 9. The callback to .every() makes sure that the input contains each digit at least once. [...Array(10).keys()]是一个 0 到 9 的数组。对.every()的回调确保输入包含每个数字至少一次。

in this solution i used include() and indexOf()在这个解决方案中,我使用了include()indexOf()

include: to verifie if test(input string) contains number from 0-9包括:验证测试(输入字符串)是否包含 0-9 的数字

indexoOf: to verifie if numbers are from 0-9 (ascending order) indexoOf:验证数字是否从 0-9(升序)

Solution:解决方案:

function Test(){
let ContAllNumber = 0; //0 = false, 1 = true

var test = "0a1b2345asd6s7e89";  
const arrayNum = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

for(let i = 0; i < test.length; i++){
    if(test.includes(i)){
        arrayNum[i] = test.indexOf(i);
    }
    else{
        break;
    }
}

for(let i = 0; i < 9; i++){
    if(arrayNum[i] < arrayNum[i+1]){
        ContAllNumber = 1; 
    }
    else{
        ContAllNumber = 0; 
        break;
    }
}   

console.log(ContAllNumber);

//condition : contains number from 0 - 9
//example respecting condition: 0123456789, 0a1b2345asd6s7e89
//example NOT respecting condition: 7193456082 , a0s93as0j4

} }

Debuggin:调试:

(I recommend you to try it to understand code better) (我建议您尝试它以更好地理解代码)

function Test(){
let ContAllNumber = 0; //0 = false, 1 = true

var test = "0123456789";  
const arrayNum = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

for(let i = 0; i < test.length; i++)
{
    if(test.includes(i))
    {
        arrayNum[i] = test.indexOf(i);
        console.log("number: " + i);
        console.log("position: " + arrayNum[i]);
    }
}

for(let i = 0; i < 9; i++)
{
    if(arrayNum[i] < arrayNum[i+1])
    {
        ContAllNumber = 1; 
        console.log("position of number " + i + " is " + arrayNum[i] + " and position of the next number " + (i + 1) + " is " + arrayNum[i+1] + "so it's respecting condition (ok)");
        console.log("so you can return 1 (true)");
    }
    else
    {
        ContAllNumber = 0; 
        console.log("position of number " + i + " is " + arrayNum[i] + " and position of the next number " + (i + 1) + " is " + arrayNum[i+1] + "it is NOT respecting condition (NOT OK)");
        console.log("so you can return 0 (false)");
        break;
    }
}   

console.log(ContAllNumber);

//condition = contains number from 0 - 9
//example respecting condition: 0123456789, 0a1b2345asd6s7e89
//example NOT respecting condition: 7193456082 , a0s93as0j4
 }

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

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