Home  • Programming • JavaScript

JavaScript program which will count how many is positive, how many negative and how many are zero numbers

Code Solutions [4384] Problem Definition Make a JavaScript program which will count how many is positive, how many negative and how many are zero numbers for the following array: var data=[4,5,-4,-1,0,-8,0,45,9]; Code
<script>
var data=[4,5,-4,-1,0,-8,0,45,9];
var positive=0;
var negative=0;
var zero=0;
for(i=0;i<data.length;i++){
 if(data[i]>0){   
   positive ++;
}else if(data[i]<0){
   negative ++;  
} else{
  zero++;
}
}//end loop
document.write("Positive Numbers: "+positive+"<br/>");
document.write("Negative Numbers: "+negative+"<br/>");
document.write("Zero Numbers: "+zero+"<br/>");
</script>
Output Positive Numbers: 4 Negative Numbers: 3 Zero Numbers: 2

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd