scrimba
Note at 0:00
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:00
AboutCommentsNotes
Note at 0:00
Expand for more info
main.js
run
preview
console
function avoidObstacles(nums) {
// If I'm understanding this correctly, all you're doing is looking for the smallest integer that's not a factor of any of the numbers in the array, right?
// So mod is your friend?
// No point in checking 1, so let's jump in with 2
// And let's have a sanity stop of 1000
// var smallest = -1;
// for (var n = 2; n < 1000; n++) {
// console.log("Guess: " + n);
// var coprime = true;
// // now to compare n with each number in nums
// for (i = 0; i < nums.length; i++) {
// console.log('nums[' + i + '] = ' + nums[i]);
// console.log(nums[i] + '%' + n + ' = ' + nums[i]%n);
// if (nums[i]%n == 0) {
// console.log("Whoops! " + n + " is a factor of " + nums[i] + "!")
// coprime = false;
// break;
// }
// }
// if (coprime == true) {
// smallest = n;
// break;
// }
// }
// return smallest;

// Ok, so that works... But now let's refactor using .every()...
// I think variables that are defined in the scope in which the function is being used don't have to be explicitly passed to the function unless I want to be able to change them... So I can use n as a test value in my coPrime function without passing it in
const isCoprime = (currentValue) => currentValue%n != 0;

// No point in checking 1, so let's jump in with 2
// And let's have a sanity stop of 1000
for (var n = 2; n < 1000; n++) {
if(nums.every(isCoprime)) {
return n;
}
}
// Let's have a fallback error message
return "Sorry -- the number must be greater than 1000";
}



/**
* 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