Explorer
project
boot.js
index.html
index.js
jasmine-html.js
jasmine.css
jasmine.js
main.js
Dependencies
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
function arrayPreviousLess(nums) {
// Null && undefined check
if (nums === null || nums === undefined) {
alert("Nothing comes from nothing")
return null
}
// Convert string to array
if (typeof(nums) === "string") nums = nums.split("")
// Check that only numbers are present
typeCheckedNums = nums.map(num => {
if (typeof(num) !== "number") num = Number(num)
if (isNaN(num)) {
return null
}
return num
})
if (typeCheckedNums.includes(null)) {
alert("Only items that are or can be converted to type number are allowed")
return null
}
// The real stuff ...
let items = typeCheckedNums.length
let backerds = typeCheckedNums.reverse()
let output = []
for (let i=0; i<items; i++) {
let sifted = backerds.filter(n => n < backerds[0])
let insert = (sifted.length === 0) ? -1 : sifted[0]
output.push(insert)
backerds.shift()
}
output.reverse()
return output
}
/**
* 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]);
});
it('alerts if null or undefined values are passed and returns null', () => {
// arrange
const nums = null;
// act
const result = arrayPreviousLess(nums);
// log
console.log("result: ", result);
// assert
expect(result).toEqual(null);
});
it('converts strings of numbers to an array', () => {
// arrange
const str = "35245";
// act
const result = arrayPreviousLess(str);
// log
console.log("result: ", result);
// assert
expect(result).toEqual([-1, 3, -1, 2, 4]);
});
it('alerts and returns null if input cannot be converted to an array of numbers', () => {
// arrange
const str = "3e24ff";
// act
const result = arrayPreviousLess(str);
// log
console.log("result: ", result);
// assert
expect(result).toEqual(null);
});
});