samedi 5 décembre 2015

How to stop this for-loop?

var number = prompt("Typ some numbers:")
var som = 0

for (var x = 0; x < number.length; x++) {
  if (!(number[x] === 0)) {
    if ((number[x] % 2) === 1) {
      som += (number[x] * number[x])
    }
  } else {
    break;
  }
}

alert(som)

I want to type some random numbers (0-9) and then it must say the som of the Square of all the odd numbers before I type zero. For example I type: 5903. 5*5 + 9*9 = 106. So AFTER I type a zero it must STOP the for loop of going further. But right now if I typ 5903 it says 115, so right now it still DOES count 3*3 extra. So how do I make it stop after I type a zero? It doesn't work right now, it goes on after I type a zero. Someone know what's the problem? Maybe syntax?

Aucun commentaire:

Enregistrer un commentaire