function smallestCommons(arr) {
let multiple = [];
for (let i = 0; i < arr.length; i++) {
if (arr.range(i,i + 1) % 2 == 0) {
multiple % arr.range(i,i + 1) === 0;
}
}
return multiple;
}
smallestCommons([1,5]);
My head is all over the place with this one. I thought I could use the range method in this way but I am doing a lot of things wrong. Can someone help me out?
I would ignore fancy syntax. You only need to express a clear, detailed plan with if and for. Fancy syntax cannot make a plan for you if you don’t have one.
I’d suggest you refactor your code by setting the conditions clear. After setting the condition, don’t forget the empty multiple array. Check string methods i.e Push()
Good luck buddy!
function smallestCommons(arr) {
for (let i = 0; i < arr.length; i++){
// if the two numbers in arr can be evenly divided
if (i % 2 == 0 && (i + 1) % 2 == 0)
// if all the numbers between them can also be evenly divided
// return the first smallest common multiple all these numbers have
}
return arr;
}
smallestCommons([1,5]);
Is this a better straightforward plan? I checked if the numbers in the arr can be evenly divided, but I am having a hard time thinking of a way to check all the numbers in between as well
function smallestCommons(arr) {
// Check all the multiples of numbers 1-5
// a multiple of a number is basically adding up by that number over and over
// for example 1:[1,2,3,4,5,6,7,8,9...]
// 2: [2,4,6,8,10...]
// 3: [3,6,9,12,15,18,21...]
// 4: [4,8,12,16,20...]
// 5: [5,10,15,20,25,30...]
// if you continued going up, eventually you would realize that 60 is the smallest common multiple for numbers 1-5
return arr;
}
smallestCommons([1,5]);
I really appreciate how blunt and honest you are. I didn’t write any code at all and wrote out the process, is this better?
Because you would see that 60 is the first number that 1,2,3,4, and 5 all have in common. Like 1-3 all have 6 in common…but you can’t say that’s the smallest common multiple because 4 and 5 don’t have 6
I can see this by looking at the pattern I have made for each number. Or do you mean like how can you see this in terms of code? Maybe by creating a loop with instructions for each number that tells the number to keep adding by it’s own value until all numbers find a number they have in common
The smallest common multiple is going to be divisible by all of those numbers (2,3,4,5). We can guess the right number will have a remainder of 0 when divided by (2,3,4,5).
We could only look at the largest number…5. Then once 5 shows us a number that is also divisible by 4,3,2…then we know that is the lowest common multiple. Instead of looking through each numbers sequence