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);
CodePudding user response:
Since I can't accurately portray in a comment what I would want to explain, I am posting an answer:
I find it much easier to balance braces when I format my code like so
function cut(input)
{
for (var i=0; i<cut.length; i )
{
if (i>0)
{
liste.push(input[i]);
return liste;
}
}
And now its pretty apparent where the unbalanced brace is.
There are other syntax errors that others have already been pointed out:
- Its not
cut.length
, ratherinput.length
. - Your
if
statement needs to beif (input[i] > 0)
, notif (i > 0)
return liste
shouldn't be inside of the loop, rather at the end of the function, because once a value is found it will stop the loop and immediately return only 1 value inside of the array.
Here should be a working example of what you intended to do. Other than those few syntax errors, good job with the logic!
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15];
function cut(input){
let liste = [];
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);
CodePudding user response:
There are 4 mistakes in your code
i<cut.length, you have to check the input length not the length of the function
i>0 , it should be input[i]>0 , since you are comparing the input indecis
Curly braces of if statement is not closed
Return is not outside the for loop
After fixing all these it should work
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 ){ //error 1
if (input[i]>0){//2
liste.push(input[i]);
}//error 3
}
return liste;//error 4
}
var result = cut(input);
console.log(result);
CodePudding user response:
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);
CodePudding user response:
for (var i=0; i<input.length; i ){
here you want to user input.length not cut.length, because you want i to go through all the indexes in the input array.
You also forgot a brace to close the for loop
In addition you are returning from inside the loop, which means the loop is exited, so as soon as the first element is found, you will exit the function, so you'll only get one element in your liste.
CodePudding user response:
Questions like these can be solved with googling a bit but for now, it seems like in your second expression, you have i<cut.length
which doesn't make sense. You have to change it to i < input.length
because you want to iterate through the input. You also can't have a return statement because you will exit out of the loop. You can also use a higher order function like the filter function to return any number that is higher than 0. input.filter((num) => num > 0)
Hope this helped!!