Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunky Monkey: I'm returning the given solution but not solving the problem #9058

Closed
gonzalezi2 opened this issue Jun 9, 2016 · 5 comments
Closed

Comments

@gonzalezi2
Copy link

Chunky Monkey

https://www.freecodecamp.com/challenges/chunky-monkey

Issue Description

The code I've written correctly follows the steps as described in the problem and I'm returning the correct solution to each instance. However, none of the instances are being checked off as correct.

Browser Information

  • Browser Name, Version: Chrome
  • Operating System: Windows 7
  • Mobile, Desktop, or Tablet: Desktop

Your Code

var segments = [];
var newArray = [];
function chunkArrayInGroups(arr, size) {
  // Break it up.
  for (var i = 0; i < arr.length; i += size) {
    segments = arr.slice(i, i+size);
    newArray.push(segments);
    console.log(newArray);
  }
  return newArray;
}


chunkArrayInGroups(["a", "b", "c", "d"], 2);

Screenshot

@erictleung
Copy link
Member

@gonzalezi2 your segments and newArray variables are in the global scope, which will keep state after each function call used in the tests. Locally scope them (i.e. put them within your function) and it should work. Happy coding!

@gonzalezi2
Copy link
Author

Wow. That's all it took. Thank you so much!!

One question: Why would it return the correct answer though? If I had gotten an error I would know to change it but without an error, and a correct answer, it's hard to figure out what the problem is.

@erictleung
Copy link
Member

@gonzalezi2 when you run it just once with the Run Tests button, it runs the entire code in your editor. However, the tests just run the function you've created and so the variables you've initialized in the global scope change with each test. Yeah, I've seen many people confused about it. Maybe we could add in a note somewhere early on in the curriculum about not using global scoped variables or something.

@gonzalezi2
Copy link
Author

yeah that would make it more clear. I still have trouble with it at times because sometimes it is necessary to create it globally because if I create a local variable and try to call it outside I get the "out of scope" error. So yeah some clarification on that would go a long way. Thanks for your help!

@ghost
Copy link

ghost commented Jun 10, 2016

Please use the Help chat room for getting challenge related help. Happy Coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants