scrimba
Note at 2:14
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 2:14
AboutCommentsNotes
Note at 2:14
Expand for more info
main.js
run
preview
console
function arrayPreviousLess(nums) {
// There is no element to the left of the first, so our first result will always be -1
result = [-1];
// Think like a for loop 😆
for (var i = 1; i < nums.length; i++) {
// Look to the left of each element
for (j = i - 1; j >= 0; j--) {
if (nums[j] < nums[i]) {
result[i] = nums[j];
// Once we've found a solution, stop looking!
break;
}
}
// If there was no satisfactory element to the left, enter -1
if (result[i] == null) {
result[i] = -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
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html
LIVE