scrimba
Note at 1:05
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:05
AboutCommentsNotes
Note at 1:05
Expand for more info
main.js
run
preview
console
function arrayPreviousLess(nums) {
let result = []
for(i=0; i < nums.length; i++) {
let everythingBefore = nums.slice(0, i)
//console.log(i + " " + nums[i] + " :" + everythingBefore)

let r = findNumLowerThan(everythingBefore, nums[i])
result[i] = r
console.log(r)
}
return result
}

function findNumLowerThan(arr, num) {
let result = -1
for (let i = 0; i < arr.length; i++) {
const elem = arr[i]
if (elem < num) {
result = elem
}
}
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
-1
,
3
,
-1
,
2
,
4
,
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html
LIVE