简体   繁体   English

如何找到第一次出现的两位数

[英]How to find a first occurrence of double digit number

So, I am pushing elements into array through prompt until getting 0. After that I am trying to find the first double digit number.所以,我通过提示将元素推入数组,直到得到 0。之后我试图找到第一个两位数。 For example if the array is [2,3,55,0] my program should return 55.例如,如果数组是[2,3,55,0]我的程序应该返回 55。

 function findFirstDouble() { var niz = [] var a = 1; for (var i = 1; a != 0; i++) { var unos = parseInt(prompt("Enter number :")) niz.push(unos) a = unos } alert(niz); for (var i = 0; i < niz.length; i++) { if (niz[i] / 10 > 0 && niz[i] / 100 == 0) { console.log(niz[i]); break; } else { alert("No double digit numbers!") break; } } } findFirstDouble();

Please use built in js function find .请使用内置的js函数find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Here is the solution这是解决方案

// I assume that you already have an array   
const niz = [2,3,55,0]
const firstDoubleDigit = niz.find(num => num < 100 && num >= 10)
console.log(firstDoubleDigit)

Easy way without math is just to convert it to a string.没有数学的简单方法就是将其转换为字符串。

 const data = [2,3,55,0]; const res = data.findIndex(n=>`${n}`.length===2); console.log(res > -1 ? "Exists at position " + res : "Doesn't exist");

Mathematically:数学上:

 const data = [2,111,3,55,0]; const res = data.find(n=>n<100&&n>9); console.log(res ? "Exists " + res : "Doesn't exist");

Here is the answer I think you are looking for.这是我认为您正在寻找的答案。

I omitted the array filling part.我省略了数组填充部分。

Why would you do any kind of division if you just need to check every number and if the first one matches the criteria then you've got your double digit number hence exit the loop with break or return keyword.如果您只需要检查每个数字,并且如果第一个数字符合条件,那么您为什么要进行任何类型的除法,那么您就得到了两位数的数字,因此使用breakreturn关键字退出循环。

var niz = [1, 2, 55, 13];
for (var i = 0; i < niz.length; i++) {
    if (niz[i] > 9 && niz[i] < 100) {
        console.log('Pronadeni broj je:', niz[i]);
        break;
    }
}

You can also convert to string: if (niz[i].toString().length===2){ // your number }你也可以转换成字符串: if (niz[i].toString().length===2){ // your number }

You use if (niz[i] / 10 > 0 && niz[i] / 100 == 0) { to determine if i is between 10 and 99 using a division.您使用if (niz[i] / 10 > 0 && niz[i] / 100 == 0) {使用除法确定 i 是否在 10 和 99 之间。

But if you want to do that, this part should be larger than one niz[i] / 10 > 0 and this part niz[i] / 100 == 0 should be smaller than one.但是如果你想这样做,这部分应该大于 1 niz[i] / 10 > 0而这部分niz[i] / 100 == 0应该小于 1。

That would give you:那会给你:

if (niz[i] / 10 >= 1 && niz[i] / 100 < 1) {

 function findFirstDouble(val) { for (var i = 0; i < val.length; i++) { if (val[i] / 10 >= 1 && val[i] / 100 < 1) { return val[i]; } } return null; } const arrays = [ [1, 2, 55, 13], [9, 1000], [1, 2] ]; arrays.forEach(s => { var res = findFirstDouble(s); console.log(res !== null ? res : "No double digit numbers in: " + s); });

But is is easier to just compare the value of niz[i] agains 10 and 100. You could update your code to let the function findFirstDouble find the first double digit number and perform the creation of the prompt values apart from that so you could reuse it.但是更容易将niz[i]的值niz[i]与 10 和 100 进行比较。您可以更新您的代码,让函数findFirstDouble找到第一个两位数并执行除此之外的提示值的创建,以便您可以重用它。

function findFirstDouble(val) {
    for (var i = 0; i < val.length; i++) {
        if (val[i] >= 10 && val[i] < 100) {
            return val[i];
        }
    }
    return null;
}

 function findFirstDouble(val) { for (var i = 0; i < val.length; i++) { if (val[i] >= 10 && val[i] < 100) { return val[i]; } } return null; } const arrays = [ [1, 2, 55, 13], [1, 2] ]; arrays.forEach(s => { var res = findFirstDouble(s); console.log(res !== null ? res : "No double digit numbers in: " + s); });

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

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