'Counting negative, positive and zero element in an array using plain javascript
I have an array:
var ar1=[];
var ar=[-1,-2,-3,0,0,5,12,0,-10];
I am trying to categorize numbers as "zeros", "negative numbers" and "positive numbers" and counting them.
This is my code:
function counter(ar) {
var num,array1=[0,0,0];
for (i=0;i<ar.length;i++) {
switch (ar[i]<0) {
case true : array1[0]++;break;
case false :
if (ar[i]=0) array1[1]++;
else array1[2]++;
break;
default : break;
}
}
return(array1);
}
Full code:
<script type="text/javascript">
function counter(ar) {
var num,array1=[0,0,0];
for (i=0;i<ar.length;i++) {
switch (ar[i]<0) {
case true : array1[0]++;break;
case false :
if (ar[i]=0) array1[1]++;
else array1[2]++;
break;
default : break;
}
}
return(array1);
}
</script></head>
<body>
<script type="text/javascript">
var ar1=[];
var ar=[-1,-2,-3,0,0,5,12,0,-10];
ar1=counter(ar);
alert("No of Negative, Zero and Positive Elements are : "+ar1);
</script>
The logic seems correct to me, but somehow it's not working. Can someone help me please.
Update
I am new in coding and just learning with javascript basics. I forgot to mention this earlier and now I see hate all over the comments.
"Is this the best way to do it?" I should have added this to my question.
I see in comments that switch case is not a good option. So what would be an alternative to that?
Solution 1:[1]
To compare you need to use ===
.
=
is used to assign a value to a variable.
Don't use a switch
in your case. You only need if else
var ar = [-1, -2, -3, 0, 0, 5, 12, 0, -10];
function counter(ar) {
var counter = [0, 0, 0];
ar.forEach(function(a) {
if (a < 0)
counter[0]++;
else if (a > 0)
counter[2]++;
else
counter[1]++;
});
return counter;
}
var result = counter(ar);
alert("No of Negative, Zero and Positive Elements are : " + result);
If you want get all the values separately, one may use object destructuring:
let [negative,zero,positive] = counter([0,0,1,1,2,2]);
if(! negative) alert("no negative ones but "+zero+" nulls");
Solution 2:[2]
You can use the filter method to count with more ease. The following code worked for me.
var arr=[-1,-2,-3,0,0,5,12,0,-10]
function myFunction(arr){
let negatives=arr.filter((el) => el < 0).length;
let zeros=arr.filter((el) => el == 0).length;
let positives =arr.filter((el) => el > 0).length;
return{
neg:negatives,
zer:zeros,
pos:positives
}
}
console.log(myFunction(arr))
Solution 3:[3]
The answer is if (ar[i] == 0) array1[1]++;
.
Solution 4:[4]
For counting positive, zero and negative number use below logic, just paste inside console you will get the results.
var ar = [-1, -2, -3, 0, 0, 5, 12, 0, -10];
var zeroCount = 0,
posCount = 0,
negativeCount = 0;
ar.forEach((item) => {
if (item === 0) {
zeroCount++
} else if (item < 0) {
negativeCount++
} else if (item > 0) {
posCount++
}
})
console.log("ZeroCount:: " + zeroCount);
console.log("PositiveCount :: " + posCount);
console.log("NegativeCount:: " + negativeCount);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rajesh |
Solution 2 | Suraj Rao |
Solution 3 | Mustansir Zia |
Solution 4 | Barmar |