简体   繁体   中英

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. For example if the array is [2,3,55,0] my program should return 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 . 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.

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 }

You use if (niz[i] / 10 > 0 && niz[i] / 100 == 0) { to determine if i is between 10 and 99 using a division.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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