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