.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
catch-arguments-passed-in-the-wrong-order-when-calling-a-function.english.md
catch-missing-open-and-closing-parenthesis-after-a-function-call.english.md
catch-misspelled-variable-and-function-names.english.md
catch-mixed-usage-of-single-and-double-quotes.english.md
catch-off-by-one-errors-when-using-indexing.english.md
catch-unclosed-parentheses-brackets-braces-and-quotes.english.md
catch-use-of-assignment-operator-instead-of-equality-operator.english.md
prevent-infinite-loops-with-a-valid-terminal-condition.english.md
understanding-the-differences-between-the-freecodecamp-and-browser-console.english.md
use-caution-when-reinitializing-variables-inside-a-loop.english.md
use-the-javascript-console-to-check-the-value-of-a-variable.english.md
use-typeof-to-check-the-type-of-a-variable.english.md
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env
* fix: add 9 solutions to challenges * fix: remove extra line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com>
92 lines
3.0 KiB
Markdown
92 lines
3.0 KiB
Markdown
---
|
|
id: 587d7b86367417b2b2512b3c
|
|
title: Use Caution When Reinitializing Variables Inside a Loop
|
|
challengeType: 1
|
|
---
|
|
|
|
## Description
|
|
<section id='description'>
|
|
Sometimes it's necessary to save information, increment counters, or re-set variables within a loop. A potential issue is when variables either should be reinitialized, and aren't, or vice versa. This is particularly dangerous if you accidentally reset the variable being used for the terminal condition, causing an infinite loop.
|
|
Printing variable values with each cycle of your loop by using <code>console.log()</code> can uncover buggy behavior related to resetting, or failing to reset a variable.
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
The following function is supposed to create a two-dimensional array with <code>m</code> rows and <code>n</code> columns of zeroes. Unfortunately, it's not producing the expected output because the <code>row</code> variable isn't being reinitialized (set back to an empty array) in the outer loop. Fix the code so it returns a correct 3x2 array of zeroes, which looks like <code>[[0, 0], [0, 0], [0, 0]]</code>.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: Your code should set the <code>matrix</code> variable to an array holding 3 rows of 2 columns of zeroes each.
|
|
testString: assert(JSON.stringify(matrix) == "[[0,0],[0,0],[0,0]]", 'Your code should set the <code>matrix</code> variable to an array holding 3 rows of 2 columns of zeroes each.');
|
|
- text: The <code>matrix</code> variable should have 3 rows.
|
|
testString: assert(matrix.length == 3, 'The <code>matrix</code> variable should have 3 rows.');
|
|
- text: The <code>matrix</code> variable should have 2 columns in each row.
|
|
testString: assert(matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2, 'The <code>matrix</code> variable should have 2 columns in each row.');
|
|
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
function zeroArray(m, n) {
|
|
// Creates a 2-D array with m rows and n columns of zeroes
|
|
let newArray = [];
|
|
let row = [];
|
|
for (let i = 0; i < m; i++) {
|
|
// Adds the m-th row into newArray
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
// Pushes n zeroes into the current row to create the columns
|
|
row.push(0);
|
|
}
|
|
// Pushes the current row, which now has n zeroes in it, to the array
|
|
newArray.push(row);
|
|
}
|
|
return newArray;
|
|
}
|
|
|
|
let matrix = zeroArray(3, 2);
|
|
console.log(matrix);
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
function zeroArray(m, n) {
|
|
// Creates a 2-D array with m rows and n columns of zeroes
|
|
let newArray = [];
|
|
for (let i = 0; i < m; i++) {
|
|
let row = [];
|
|
// Adds the m-th row into newArray
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
// Pushes n zeroes into the current row to create the columns
|
|
row.push(0);
|
|
}
|
|
// Pushes the current row, which now has n zeroes in it, to the array
|
|
newArray.push(row);
|
|
}
|
|
return newArray;
|
|
}
|
|
|
|
let matrix = zeroArray(3, 2);
|
|
console.log(matrix);
|
|
```
|
|
</section>
|