scrimba
Note at 1:30
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

Note at 1:30
AboutCommentsNotes
Note at 1:30
Expand for more info
main.js
run
preview
console
function arrayPreviousLess(nums) {
// Double for-loop: O(N^2) time complexity O(1) space
for (let i = nums.length - 1; i >= 0; i--) {
for (let j = i - 1; j >= 0; j--) {
if (nums[j] < nums[i]) {
nums[i] = nums[j]
break
} else if (j == 0) {
nums[i] = -1
}
}
}
nums[0] = -1 // no numbers to left, final step after loop
return nums
}



/**
* 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
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html
LIVE