i’m totally new to coding: i want to iterate through the array input, select the positive numbers only, then put them in a new array liste and then print the new array in the console. what am i doing wrong here?!?
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]; var liste = []; function cut(input){ for (var i=0; i<cut.length; i++){ if (i>0){ liste.push(input[i]); return liste; } } var result = cut(input); console.log(result);
Advertisement
Answer
I’ll attempt to combine the points made in several other answers here, and add some more explanation.
Fixing the For Loop, One Line at a Time
Most of your code is fine; the only issues are in your for
loop. Let’s review that from top to bottom.
for (var i=0; i<cut.length; i++){
You have the right idea here. However, in this for
loop, you want to make i
loop from 0 to the length of the array you’re looping over– not the length of the function you wrote. So you should replace cut.length
with input.length
. This way, i
will loop from 0 to 14.
if (i>0){
i
is a number you’re using to keep track of how far into the array you are. As mentioned above, for your array, it will go from 0 to 14. You’re trying to check if the number at the i
th position is positive, not if i
itself is. To access the number at the i
th position, you can use input[i]
instead of just i
.
liste.push(input[i]);
This line is fine; nice work! You’re finding the number at the i
th position of the input
array and adding it to liste
. Because of the if
statement before, this only happens when that number is positive.
return liste;
This line will return the list immediately, exiting your cut
function. You want this to happen only after you’re done looping through all the numbers, so you just need to move this line after the for
loop.
And one last thing– you forgot a curly brace to end your if
statement. Be careful with this, as it can mess up your program.
I’ve gone ahead and made all these changes. You can check them out in the following snippet:
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]; var liste = []; function cut(input){ for (var i=0; i<input.length; i++){ if (input[i]>0){ liste.push(input[i]); } } return liste; } var result = cut(input); console.log(result);
Some things to explore:
- What happens if you move
var liste = [];
insidecut
, like in Shmack’s answer? Does the code still work? Why? - What happens if you rename all
input
s insidecut
to something else? Does the code still work? Why?
Understanding these questions isn’t necessary, but learning the answers may help you to get better at coding for the future.
A Better Method
But wait, there’s more! What if there was a built-in feature that could do this more easily for us?
Introducing filter!
filter
is a useful method that all arrays have that lets them filter their contents based on a function that you give them. Using filter
lets you bypass writing a for
loop at all (although it is still good to practice writing them; sometimes they are very useful).
The function you provide to filter
is usually written as an “arrow function”, which basically just means turning this:
function(input){ //Do stuff return output; }
into this:
(input) => { //Do stuff return output; }
It’s very useful for writing quick little functions, so I’ll use it in my example.
To filter the array using filter
and arrow functions, all you have to do is this:
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]; var result = input.filter((number)=>{return number > 0}); console.log(result);