jeudi 31 janvier 2019

Having trouble counting iterations of different outputs in my javascript code

Trying to set up code to take inputted numbers, determine whether they are negative or positive, displaying this, and then when 0 is put in the code displays the amount of positive numbers put in and the amount of negative numbers put in and then terminates. Having trouble with the counting part and I am not sure how to set this up better. I am also not sure how to set this up to terminate after 0 is put in.

function mapping() {

 var num=parseInt(document.getElementById("num1").value);

 var countp = 0
 var countn = 0



if (num!==0) {

  if (num>0){

  document.getElementById("output").innerHTML="positive"
  countp +=1;

} else {

  document.getElementById("output").innerHTML="negative"
  countn += 1

}}  else {

document.getElementById("output").innerHTML="countp: " + countp;
document.getElementById("output").innerHTML="countn: " + countn;
}


}

Thank you.

Aucun commentaire:

Enregistrer un commentaire