function arrayPreviousLess(nums) {
// write code here.
const result = [];
const cycledNums = []; //stores previously iterated numbers
//cycles through all previously iterated numbers, provides the last found smallest number back
else -1 if not found
function checkGreater(number){
let lastSmallerNum = -1; //initiate at -1 for first loop with nothing to compare
for (let j = 0; j < cycledNums.length; j++){
lastSmallerNum = number > cycledNums[j]? cycledNums[j] : -1;
}
return lastSmallerNum;
}
//iterates over each provided number, calls above function then adds current number to cycled
array
for( let i = 0; i < nums.length; i++){
result.push((checkGreater(nums[i])));
cycledNums.push(nums[i]);
}
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]);
});
});