scrimba
Solution for day 3 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for day 3 of #javascriptmas
AboutCommentsNotes
Solution for day 3 of #javascriptmas
Expand for more info
main.js
run
preview
console
function chunkyMonkey(values, size) {
let chunkedArray = []
let chunkStartIdx = 0
let chunkEndIdx = size
// chunk array until end pointer is past end of values
while (chunkEndIdx <= values.length) {
chunkedArray.push(values.slice(chunkStartIdx, chunkEndIdx)) // not inclusive end
chunkStartIdx += size
chunkEndIdx += size
}
// now handle residuals - if any
// if no residual, end pointer should be exactly 1 chunk past
if (chunkEndIdx !== values.length + size) {
chunkedArray.push(values.slice(chunkStartIdx, values.length))
}
console.log(chunkedArray)
return chunkedArray


}



/**
* Test Suite
*/
describe('chunkyMonkey()', () => {
it('returns largest positive integer possible for digit count', () => {
// arrange
const values = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
const size = 4;

// act
const result = chunkyMonkey(values, size);

// log
console.log("result: ", result);

// assert
expect(result).toEqual([["a", "b", "c", "d"], ["e", "f", "g", "h"], ["i"]]);
});
});
Console
[
[
"a"
,
"b"
,
"c"
,
"d"
]
,
[
"e"
,
"f"
,
"g"
,
"h"
]
,
[
"i"
]
]
,
"result: "
,
[
[
"a"
,
"b"
,
"c"
,
"d"
]
,
[
"e"
,
"f"
,
"g"
,
"h"
]
,
[
"i"
]
]
,
/index.html
LIVE