scrimba
Note at 0:54
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 0:54
AboutCommentsNotes
Note at 0:54
Expand for more info
main.js
run
preview
console
function avoidObstacles(nums) {
// write code here.
//currently searches through all available values up to max but as this begins from the smallest value possible upwards (1) it can simply return after first multiple match returns false

//sort then assign last value to max value possible
const sortedArray = nums.sort();
const maxValue = sortedArray[sortedArray.length-1];

//assign min increment allowed to max possible value
let minIncrement = maxValue;

//loop through all values up until max
for(let i = 1; i < maxValue+1; i++){

//pass iterator to return all multiples up to max
let iteratorMultiples = getMultipleArray(i,maxValue);

//boolean check if any items in multiple array match a value in original array
const doAnyMultiplesMatch = iteratorMultiples.some((multiple) => {
//runs for each item, returns true if matched
return sortedArray.includes(multiple);
})

//if boolean check false and iterator is less than current min value assign current iterator
!doAnyMultiplesMatch && i < minIncrement? minIncrement = i: false;

}

//get all multiples of provided number up until max
function getMultipleArray(number, max){
const multiples = [];
//copy provided number to iterator, increment by original number
for(let i = number; i <= max; i += number){
multiples.push(i);
}
return multiples;
}

//return value
return minIncrement;
}



/**
* Test Suite
*/
describe('avoidObstacles()', () => {
it('returns minimal number of jumps in between numbers', () => {
// arrange
const nums = [5, 3, 6, 7, 9];

// act
const result = avoidObstacles(nums);

// log
console.log("result: ", result);

// assert
expect(result).toBe(4);
});
});
Console
"result: "
,
4
,
/index.html
LIVE