Table of Contents
show
Example 20
Write a JavaScript that reads 10 numbers from user and store it in array. Display the count of negative numbers, positive numbers and zero from the list.
Code
const prompt = require("prompt-sync")();
const input = parseInt(prompt("Enter the number of elements : "));
let a = new Array();
let pos_count = 0;
let neg_count = 0;
let zero_count = 0;
for (let i = 0; i < input; i++) {
let t = prompt("enter the number for \t" + i + " \t location \t");
a[i] = parseInt(t); // convert string to integer
}
console.log("Elements are:");
for (let i = 0; i < input; i++) {
console.log("a[" + i + "]:" + a[i]);
}
for (let i = 0; i < input; i++) {
if (a[i] > 0) {
pos_count++;
} else if (a[i] < 0) {
neg_count++;
} else {
zero_count++;
}
}
console.log(`positives : ${pos_count}`);
console.log(`negatives : ${neg_count} `);
console.log(`zeros : ${zero_count}`);
Output
Views: 104