Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.md
access-property-names-with-bracket-notation.md
add-items-to-an-array-with-push-and-unshift.md
add-items-using-splice.md
add-key-value-pairs-to-javascript-objects.md
check-for-the-presence-of-an-element-with-indexof.md
check-if-an-object-has-a-property.md
combine-arrays-with-the-spread-operator.md
copy-an-array-with-the-spread-operator.md
copy-array-items-using-slice.md
create-complex-multi-dimensional-arrays.md
generate-an-array-of-all-object-keys-with-object.keys.md
iterate-through-all-an-arrays-items-using-for-loops.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.md
modify-an-array-stored-in-an-object.md
modify-an-object-nested-within-an-object.md
remove-items-from-an-array-with-pop-and-shift.md
remove-items-using-splice.md
use-an-array-to-store-a-collection-of-data.md
use-the-delete-keyword-to-remove-object-properties.md
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/copy-an-array-with-the-spread-operator.md
Mykyta Ivanchenko cc464a6e56 fix(learn): Possible semantic error in explanation comment ()
* Possible semantic error

"identical" generally means that both variables are linked to exact same array which is not the case:
``` thisArray !== thatArray ```

Maybe "similar" or other word could be used instead

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/copy-an-array-with-the-spread-operator.md

Made the comment more accurate

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
2020-10-14 19:53:49 -06:00

3.0 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7b7b367417b2b2512b13 Copy an Array with the Spread Operator 1 301157

Description

While slice() allows us to be selective about what elements of an array to copy, among several other useful tasks, ES6's new spread operator allows us to easily copy all of an array's elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this: ... In practice, we can use the spread operator to copy an array like so:
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];
// thatArray equals [true, true, undefined, false, null]
// thisArray remains unchanged and thatArray contains the same elements as thisArray

Instructions

We have defined a function, copyMachine which takes arr (an array) and num (a number) as arguments. The function is supposed to return a new array made up of num copies of arr. We have done most of the work for you, but it doesn't work quite right yet. Modify the function using spread syntax so that it works correctly (hint: another method we have already covered might come in handy here!).

Tests

tests:
  - text: <code>copyMachine([true, false, true], 2)</code> should return <code>[[true, false, true], [true, false, true]]</code>
    testString: assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]]);
  - text: <code>copyMachine([1, 2, 3], 5)</code> should return <code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>
    testString: assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]);
  - text: <code>copyMachine([true, true, null], 1)</code> should return <code>[[true, true, null]]</code>
    testString: assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
  - text: <code>copyMachine(["it works"], 3)</code> should return <code>[["it works"], ["it works"], ["it works"]]</code>
    testString: assert.deepEqual(copyMachine(['it works'], 3), [['it works'], ['it works'], ['it works']]);
  - text: The <code>copyMachine</code> function should utilize the <code>spread operator</code> with array <code>arr</code>
    testString: assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));

Challenge Seed

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // Only change code below this line

    // Only change code above this line
    num--;
  }
  return newArr;
}

console.log(copyMachine([true, false, true], 2));

Solution

function copyMachine(arr,num){
	let newArr=[];
	while(num >=1){
	newArr.push([...arr]);
	num--;
	}
	return newArr;
}
console.log(copyMachine([true, false, true], 2));