Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Daan Schouten
Daan Schouten
14,454 Points

Function reassigns argument that is passed to it unintentionally. (Node.js / Mongo )

I'm writing a program using Epxress, Node and Mongo that generates sudoku's and allows people to fill it in. In the code below, I first run the genSud() method, which generates a complete sudoku. Then, I run the emptySud() method, which takes the complete sudoku from genSud(), and a difficulty level. A difficulty level of 20 would mean the emptySud() method takes out 20 numbers from the full sudoku. After this, I try to add both the half-empty sudoku (for showing to the user), and the full sudoku (for checking the user's input) to the database. However, for some reason, when I pass the full Sudoku into the emptySud() method, the full Sudoku takes on the value of the ultimate result of emptySud(). Essentially, I end up with twice the half sudoku in the database, rather than the full one and the half one.

I suspect this has to do with linkage, which is why I use "use strict", but to no avail. Any ideas? i have created a CodePen that allows you to see how the code runs for itself. The genSud() returns a full sudoku, the emptySud() removes some numbers. As you can see, they run fine separately, but somehow I can't get them to work together whilst saving both a full and a half sudoku.

https://fiddle.jshell.net/pjxowsdh/

The code below only shows how I run the functions from the main 'app.js'.

 let completeSudoku = sudoku.genSud();
 // console.log(completeSudoku) returns a full sudoku
 let tempSudoku = completeSudoku;
 // console.log(completeSudoku) returns a full sudoku
 let halfSudoku = sudoku.emptySud(tempSudoku, difficulty);
 // console.log(completeSudoku) returns the half sudoku