fix(curriculum): simplify the Remove Items Using splice() challenge without using reduce (#37829)

* fix: simplify challenge without using reduce

* fix: prevent user from adding new elements to sum to 10

* fix:  add from

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>
This commit is contained in:
Randell Dawson
2019-11-26 12:14:44 -08:00
committed by Manish Giri
parent 1b61bceee7
commit ba415184f6

View File

@ -31,7 +31,9 @@ let newArray = array.splice(3, 2);
## Instructions ## Instructions
<section id='instructions'> <section id='instructions'>
We've defined a function, <code>sumOfTen</code>, which takes an array as an argument and returns the sum of that array's elements. Modify the function, using <code>splice()</code>, so that it returns a value of <code>10</code>.
We've initialized an array `arr`. Use `splice()` to remove elements from `arr`, so that it only contains elements that sum to the value of <code>10</code>.
</section> </section>
## Tests ## Tests
@ -39,11 +41,14 @@ We've defined a function, <code>sumOfTen</code>, which takes an array as an argu
```yml ```yml
tests: tests:
- text: <code>sumOfTen</code> should return 10 - text: You should not change the original line of <code>const arr = [2, 4, 5, 1, 7, 5, 2, 1];</code>.
testString: assert.strictEqual(sumOfTen([2, 5, 1, 5, 2, 1]), 10); testString: assert(code.replace(/\s/g, '').match(/constarr=\[2,4,5,1,7,5,2,1\];?/));
- text: The <code>sumOfTen</code> function should utilize the <code>splice()</code> method - text: <code>arr</code> should only contain elements that sum to <code>10</code>.
testString: assert.notStrictEqual(sumOfTen.toString().search(/\.splice\(/), -1); testString: assert.strictEqual(arr.reduce((a, b) => a + b), 10);
- text: Your code should utilize the <code>splice()</code> method on <code>arr</code>.
testString: assert(code.replace(/\s/g, '').match(/arr\.splice\(/));
- text: The splice should only remove elements from <code>arr</code> and not add any additional elements to <code>arr</code>.
testString: assert(!code.replace(/\s/g, '').match(/arr\.splice\(\d+,\d+,\d+.*\)/g));
``` ```
</section> </section>
@ -54,15 +59,11 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sumOfTen(arr) { const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// change code below this line // only change code below this line
// change code above this line // only change code above this line
return arr.reduce((a, b) => a + b); console.log(arr);
}
// do not change code below this line
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
``` ```
</div> </div>
@ -75,10 +76,8 @@ console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
<section id='solution'> <section id='solution'>
```js ```js
function sumOfTen(arr) { const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(2,2); arr.splice(1, 4);
return arr.reduce((a, b) => a + b);
}
``` ```
</section> </section>