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 array = [];

nums.reverse().forEach((value, index) => {
for (let i = index; i < nums.length; i++) {
if (value > nums[i]) return array.unshift(nums[i]);
}
array.unshift(-1)
});

return array;
}



/**
* 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]);
});

it("shift previous postions from the left to a smaller value or store -1", () => {
// arrange
const nums = [9, 5, 2, 8, 11, 1, 6];

// act
const result = arrayPreviousLess(nums);

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

// assert
expect(result).toEqual([-1, -1, -1, 2, 8, -1, 1]);
});

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

// act
const result = arrayPreviousLess(nums);

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

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