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 ments.
"Is this the best way to do it?" I should have added this to my question.
I see in ments that switch case is not a good option. So what would be an alternative to that?
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 ments.
"Is this the best way to do it?" I should have added this to my question.
I see in ments that switch case is not a good option. So what would be an alternative to that?
Share Improve this question edited Sep 1, 2017 at 5:31 asked Sep 1, 2017 at 5:13 user8543943user8543943 2-
1
use ==
(ar[i]==0)
– Saurabh Commented Sep 1, 2017 at 5:15 - This might help: codecademy./en/forum_questions/558ea4f5e39efed371000508 – Rajesh Commented Sep 1, 2017 at 5:30
5 Answers
Reset to default 3To pare 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");
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))
The answer is if (ar[i] == 0) array1[1]++;
.
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);
function myTest(a){
return a.filter((e) =>(e <0).length)
}
myTest([1,-2,2,-4])