scrimba
Note at 2:15
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:15
by
AboutCommentsNotes
Note at 2:15
by
Expand for more info
main.js
run
preview
console
const arrayPreviousLess = (nums)=>{
const store = [];
nums.map((num, index) => {
let minusFirst = index <= 0?store.unshift(-1):nums // add the first -1 before starting loop

for (let i = index - 1; i >= 0; i--) {// start at the end of the array and compare back -1
if (nums[i] < nums[index]) {
console.log(store)
return store[store.length]=nums[i]

} else if (i === 0) {

return store[store.length]=-1 // items left then change to -1

}
}
})
return store
}




// const arrayPreviousLess=(nums)=> {
// let store=[]
// nums.reverse().map((item,index)=>{
// item > nums[index +1]
// ?store.unshift(nums[index+1])
// :store.unshift(-1)
// })
// return store
// }


//return numbers less than current number to the left, if number is not less return -1




/**
* 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
]
,
[
-1
,
3
,
-1
]
,
[
-1
,
3
,
-1
,
2
]
,
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html?spec=arrayPreviousLess()%20shift%20previous%20postions%20from%20the%20left%20to%20a%20smaller%20value%20or%20store%20-1
LIVE