mardi 23 juin 2020

how to get last occurance of element m in array n

I just started with programming and I am just trying to access the only last position of element m in array n.The code given below provide the all positions of element m.

 var n = [];
        while (true) {
            let input = prompt("enter number to array");
            if (input == null || input == "c") {
                break;
            }
            n.push(Number(input));
            console.log(n);
        }
        var m = prompt("enter number to be match");
        console.log(m)
        for (var i = 0; i < n.length; i++) {
            if (n[i] == m) {
                console.log(i);
            } 

}

Aucun commentaire:

Enregistrer un commentaire