Having a little bit of trouble understanding why this won’t work…
function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (i % 2 ===0){
arr.push(array[i]);
}
return arr;
}
}
Having a little bit of trouble understanding why this won’t work…
function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (i % 2 ===0){
arr.push(array[i]);
}
return arr;
}
}
You’re pushing every second value in the array, not every even numbered value
> question0([1,3,1,3,1,3])
[3,3,3]
Edit also what @ArielLeslie said
As soon as a return
statement is reached, function execution ends. Your return
statement is inside your for
loop, so only the first value of array
is checked before the function returns.
function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (i % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}
Yeah, fixed that, but still stuck here.
See my answer, you aren’t checking what the values in the array are
I don’t understand what you are trying to tell me. I mean, I see what you typed, but how the hell is it printing every second? I’m iterating through the array.
I could’ve swore this code was right and I’m stuck. I don’t understand how it doesn’t run. I’m iterating through the array and checking if the index is divisible by 2. If it is, push it into the new array…
This can’t be it…this is it?
function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (array[i] % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}
Seems to be. Ugh, this is what I had first, too! I just switched up other lines previously. Thanks
Get a pen and a piece of paper. write your logic of the function and try to do by pen and paper what your function does. keep an eye on values and see where your code is broken. just think logical. @camperextraordinaire and @DanCouper are pointing you the right way. you just have to see it.
Still got a bit of a problem. It is telling me this in the terminal:
Well, this entire section is wanting me to test the results in the terminal and commit to Github.
This is all the directions I get:
// Write a for loop that will print only even numbers from an array:
// For example: question0([ 5, 20, 11, 42, 2, 19 ]) >> [ 20, 42, 2 ]
function question0(array){
let arr = [];
for (let i = 0; i < array.length; i++){
if (array[i] % 2 === 0){
arr.push(array[i]);
}
}
return arr;
}
incoming array is
VM49:3 (6) [5, 20, 11, 42, 2, 19]0: 51: 202: 113: 424: 25: 19length: 6__proto__: Array(0)
(3) [20, 42, 2]
Usually I am, but this portion is involving Git and checking the code in the Terminal. The section involves 3 problems (question0, question1, question2) and it seems like I passed the other two, but the first one (this thread) doesn’t seem to pass:
It’s funny, 'cause on line 18, it’s only the directions for the next problem. (They require you to write in Atom, usually I like VS Code.)
If I’m not mistaken, I believe so. I have waited too long to learn Git and now it has become a regret of mine.
Yes, it is. Typical fork and clone repository, solve functions, test in terminal, then commit back.
“npm test”
This is of course after I “cd” into the directories and get to the file.
I don’t see how this is anything to do with git. The test isn’t passing, so you need to see why that is the case, git has nothing to do with it. The tests are definitely there and the tests are running, but you need to find out what the test is actually testing for.