0

New to js and I'm attempting to solve a really simple problem. I'm not sure why it's not working though. Must be a js quirk I am unfamiliar with. Can someone tell me why x would return back as undefined thus causing my function to return false when it should return true? I'm trying to replicate the 'every' method for arrays and return false if one of the array elements returns false from my callback.

I've attempted to debug with webstorm and I still could not find the solution. Here is my code,

function every_loop(array, test) {
  for (let index = 0; array.length - 1; index++) {
    let x = test(array[index]);
    if (!x) {
      console.log(array[index], index);
      return false;
    }
  }
  return true;
}

console.log(every_loop([1, 2, 3, 4, 5], n => n >= 1));

My output is false when it should be true. Also, right before it outputs false, it shows undefined as a value for array[index] which leads me to believe that my for loop parameters are incorrect but that isn't the case either. Any help would be appreciated. Thanks

4
  • 3
    Well where's the test() code? Also your for loop test condition is wrong. edit oh wait I see, sorry Commented Jan 24, 2019 at 16:42
  • @Pointy Yea for some reason, no matter what I change the 2nd parameter in for loop to, say array.length -2, it still goes through the array one more time than it should and tries to find a fifth index. this is undefined. I don't know why it is doing that though Commented Jan 24, 2019 at 16:45
  • 1
    The second expression has to be a comparison, not just a number. Commented Jan 24, 2019 at 16:46
  • @UCProgrammer You would notice the problem if you log index as well. It is getting out of bound. Commented Jan 24, 2019 at 16:47

4 Answers 4

2

you are missing the clause of the second parameter of the for loop. changing it to index <= array.length - 1 fixes your code.

function every_loop(array, test) {
  for (let index = 0; index <= array.length - 1; index++) {
    let x = test(array[index]);
    if (!x) {
      console.log(array[index]);
      return false;
    }
  }
  return true;
}

console.log(every_loop([1, 2, 3, 4, 5], n => n >= 1));

Sign up to request clarification or add additional context in comments.

2 Comments

jeez louis. thanks. Should I delete this question? I doubt anyone would be as stupid as me and forget that. Been answered tons of times before I assume
@UCProgrammer See this
1

Your for loop is broken. I would recommend for..of -

function everyLoop (arr, test) {
  for (const x of arr)
    if (!test(x))
      return false
  return true
}

console.log(everyLoop([1,2,3,4,5], n => n >= 1)) // true
console.log(everyLoop([1,2,3,4,5], n => n >= 2)) // false

You said it's practice, but just know JavaScript does include a built-in Array#every that does exactly this -

console.log([1,2,3,4,5].every(n => n >= 1)) // true
console.log([1,2,3,4,5].every(n => n >= 2)) // false

2 Comments

thanks and I'm aware. part of the problem was to do it both ways and did the solution with every method in a second's time and then switched to the loop and thought it would be simple as well. Little did I know I'd forget something as simple as a condition on my 2nd parameter in for loop and end up spending 20 minutes on the problem
Try not to beat yourself up. JavaScript is a loose, multi-paradigm language making it possible to solve the same problem in countless ways. It's difficult for learners to find the important things to focus on. Other languages have better tooling that would make it easier to spot this bug, or make it altogether impossible to even write the bug (compile error).
1

Your condition in for loop will be always true. (array.length-1==4). So use this instead:

function every_loop(array, test) {
        for (let index = 0; index<array.length;index++) {
            let x = test(array[index]);
            if (!x) {
                return false;
            }
    }
    return true;
}

console.log(every_loop([1,2,3,4,5], n => n >= 1));
console.log(every_loop([0,1,2,3,4,5], n => n >= 1));

Comments

1

replace

for (let index = 0; array.length - 1; index++) {

with

for (let index = 0; index < array.length; index++) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.