I have created an Array with some numbers. I want to find out how many even, and how many odd numbers it is in this Array. I have to print it out like this: (this is just an example)
Even number: 6
Odd number: 7
I need to make a loop that count up how many it is off even and odd numbers.
This is what I have so far
<script>
window.onload = run;
var tall = [5,10,15,20,25,30,35,40,45,50];
function run() {
tall = [5,10,15,20,25,30,35,40,45,50];
liste(tall);
}
function liste(arr) {
var sumOdd = 0; // Odd 1, 3, 5 etc..
var sumPar = 0; // Even 2, 4, 6 etc..
for(var i = 0; i < arr.length; i++) {
if(arr[i] % 2 === 0) {
sumPar += arr.length;
}
else {
sumOdd += arr.length;
} // Even numbers // Odd numbers
document.getElementById("print").innerHTML = "Partall: " + sumPar + "<br />" + "Oddetall: " + sumOdd;
}
}
}
</script>
Its something that is wrong here, and I dont know what.
I have created an Array with some numbers. I want to find out how many even, and how many odd numbers it is in this Array. I have to print it out like this: (this is just an example)
Even number: 6
Odd number: 7
I need to make a loop that count up how many it is off even and odd numbers.
This is what I have so far
<script>
window.onload = run;
var tall = [5,10,15,20,25,30,35,40,45,50];
function run() {
tall = [5,10,15,20,25,30,35,40,45,50];
liste(tall);
}
function liste(arr) {
var sumOdd = 0; // Odd 1, 3, 5 etc..
var sumPar = 0; // Even 2, 4, 6 etc..
for(var i = 0; i < arr.length; i++) {
if(arr[i] % 2 === 0) {
sumPar += arr.length;
}
else {
sumOdd += arr.length;
} // Even numbers // Odd numbers
document.getElementById("print").innerHTML = "Partall: " + sumPar + "<br />" + "Oddetall: " + sumOdd;
}
}
}
</script>
Its something that is wrong here, and I dont know what.
Share Improve this question edited Oct 28, 2016 at 9:44 Mistalis 18.3k14 gold badges77 silver badges97 bronze badges asked Oct 28, 2016 at 9:30 celllaa95celllaa95 351 silver badge9 bronze badges 4-
+= arr.length
makes no sense. You just want+= 1
, or++
. – deceze ♦ Commented Oct 28, 2016 at 9:32 - Why are you adding the length of the array each time? If you want to count the number of elements that are odd or even, add one each time, if you want to add the value itself each time then do arr[i]. – DibsyJr Commented Oct 28, 2016 at 9:33
- Don't you add the length of tall (so 10) every time you find a number? You would just need to add 1 every time you find one, not the length of the array. – Lehue Commented Oct 28, 2016 at 9:33
- I have been trying differents things. I started to try the arr[i], but the result was that they were summed together, and thats not what I wanted – celllaa95 Commented Oct 28, 2016 at 9:36
3 Answers
Reset to default 5You could iterate with Array#reduce
and count only the odds. For the rest just take the difference of the length of the array and the odds.
var tall = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
odd = tall.reduce(function (r, a) { return r + a % 2; }, 0),
even = tall.length - odd;
console.log('odd', odd);
console.log('even', even);
You were adding arr.length
which is the array length. Instead you should simply increment the number
var tall = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50];
liste(tall);
function liste(arr) {
var sumOdd = 0;
var sumPar = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
sumPar++;
} else {
sumOdd++;
}
}
console.log("Odd : " + sumOdd);
console.log("Par : " + sumPar);
}
You always add the plete Length of the array to your variable
Try this instead of sumPar += arr.length;
:
sumPar++;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744749629a4591513.html
评论列表(0条)