* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d78b2367417b2b2512b0e | Add Items to an Array with push() and unshift() | 1 | 301151 | add-items-to-an-array-with-push-and-unshift |
--description--
An array's length, like the data types it can contain, is not fixed. Arrays can be defined with a length of any number of elements, and elements can be added or removed over time; in other words, arrays are mutable. In this challenge, we will look at two methods with which we can programmatically modify an array: Array.push()
and Array.unshift()
.
Both methods take one or more elements as parameters and add those elements to the array the method is being called on; the push()
method adds elements to the end of an array, and unshift()
adds elements to the beginning. Consider the following:
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];
romanNumerals.unshift('XIX', 'XX');
romanNumerals
would have the value ['XIX', 'XX', 'XXI', 'XXII']
.
romanNumerals.push(twentyThree);
romanNumerals
would have the value ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']
. Notice that we can also pass variables, which allows us even greater flexibility in dynamically modifying our array's data.
--instructions--
We have defined a function, mixedNumbers
, which we are passing an array as an argument. Modify the function by using push()
and unshift()
to add 'I', 2, 'three'
to the beginning of the array and 7, 'VIII', 9
to the end so that the returned array contains representations of the numbers 1-9 in order.
--hints--
mixedNumbers(["IV", 5, "six"])
should now return ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]
assert.deepEqual(mixedNumbers(['IV', 5, 'six']), [
'I',
2,
'three',
'IV',
5,
'six',
7,
'VIII',
9
]);
The mixedNumbers
function should utilize the push()
method
assert(mixedNumbers.toString().match(/\.push/));
The mixedNumbers
function should utilize the unshift()
method
assert(mixedNumbers.toString().match(/\.unshift/));
--seed--
--seed-contents--
function mixedNumbers(arr) {
// Only change code below this line
// Only change code above this line
return arr;
}
console.log(mixedNumbers(['IV', 5, 'six']));
--solutions--
function mixedNumbers(arr) {
arr.push(7,'VIII',9);
arr.unshift('I',2,'three');
return arr;
}