scrimba
Note at 2:42
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:42
AboutCommentsNotes
Note at 2:42
Expand for more info
main.js
run
preview
console
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);
});
});
Console
[
"3"
,
"5"
,
"2"
,
"4"
,
"5"
]
,
3
,
5
,
2
,
4
,
5
,
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
"result: "
,
null
,
[
"3"
,
"e"
,
"2"
,
"4"
,
"f"
,
"f"
]
,
3
,
null
,
2
,
4
,
null
,
null
,
"result: "
,
null
,
[
3
,
5
,
2
,
4
,
5
]
,
3
,
5
,
2
,
4
,
5
,
"result: "
,
[
-1
,
3
,
-1
,
2
,
4
]
,
/index.html
LIVE