scrimba
Solution for day 18 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 18 of #javascriptmas
AboutCommentsNotes
Solution for day 18 of #javascriptmas
Expand for more info
main.js
run
preview
console
function arrayPreviousLess(nums) {
let result = [];
for (let i = nums.length - 1, j = i - 1; i >= 0; i--, j--) {
if (nums[j] < nums[i]) {
result.unshift(nums[j]);
} else {
result.unshift(-1);
}
}
return result;
}



/**
* Test Suite
*/
describe('arrayPreviousLess()', () => {
it('shift previous postions from the left to a smaller value or store -1', () => {
// arrange
const nums = [3, 5, 2, 4, 5];

// act
const result = arrayPreviousLess(nums);

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

// assert
expect(result).toEqual([-1, 3, -1, 2, 4]);
});
});
Console
"5"
,
4
,
true
,
"4"
,
2
,
true
,
"2"
,
5
,
false
,
"5"
,
3
,
true
,
"3"
,
undefined
,
false
,
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html
LIVE