Stop Loop For when a certain number is typed

Asked

Viewed 31 times

-2

I’m trying to stop a loop for when the number 42 is typed. Even my code is like this:

var numero = prompt('Digite o numero')
var arrNumero  = []
arrNumero.push(numero)
console.log(arrNumero)

for (let i = 0; i < arrNumero.length; i++) {
 if (arrNumero[i] === '42') {
    break;
 } else {
        console.log(arrNumero[i])
 }
} 

When you enter the number 42 for no stop with the break instruction. I would like to know from you how to not print the number 42 on the console.

  • From what I understand, it wouldn’t just be removing the console.log. Below the line : arrNumero.push(numero)?

1 answer

0


I ran your code and it works normally, so it’s even easier to see:

var numero = prompt('Digite o numero')
var arrNumero  = [1,2,3,numero,5,6,7]
console.log(arrNumero)

for (let i = 0; i < arrNumero.length; i++) {
 if (arrNumero[i] === '42') {
    break;
 } else {
        console.log(arrNumero[i])
 }
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.