function arrayPreviousLess(nums) {
// the first element will always be -1 since it doesn't have a previous element
let smallerPrevious = [-1];
// for all other elements identify smallest previous value or -1
for (let i=1; i<nums.length; i++) {
j = i - 1;
while (nums[j] >= nums[i] && j > 0) {
j--;
}
nums[j] < nums[i] ? smallerPrevious.push(nums[j]) : smallerPrevious.push(-1);
}
return smallerPrevious;
}
/**
* 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]);
});
});