Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

2.4 KiB

id, title, challengeType
id title challengeType
587d7da9367417b2b2512b66 Combine Two Arrays Using the concat Method 1

Description

Concatenation means to join items end to end. JavaScript offers the concat method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to concat, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:
[1, 2, 3].concat([4, 5, 6]);
// Returns a new array [1, 2, 3, 4, 5, 6]

Instructions

Use the concat method in the nonMutatingConcat function to concatenate attach to the end of original. The function should return the concatenated array.

Tests

tests:
  - text: Your code should use the <code>concat</code> method.
    testString: assert(code.match(/\.concat/g), 'Your code should use the <code>concat</code> method.');
  - text: The <code>first</code> array should not change.
    testString: assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The <code>first</code> array should not change.');
  - text: The <code>second</code> array should not change.
    testString: assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The <code>second</code> array should not change.');
  - text: <code>nonMutatingConcat([1, 2, 3], [4, 5])</code> should return <code>[1, 2, 3, 4, 5]</code>.
    testString: assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), '<code>nonMutatingConcat([1, 2, 3], [4, 5])</code> should return <code>[1, 2, 3, 4, 5]</code>.');

Challenge Seed

function nonMutatingConcat(original, attach) {
  // Add your code below this line


  // Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);

Solution

function nonMutatingConcat(original, attach) {
  // Add your code below this line
  return original.concat(attach);
  // Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);