I have managed to get passed most of the intermediate challenges but these last 3 maths-based ones I have struggled.
Is there anything I can research to help me understand what I’m doing without looking directly at the solution. So far I have sorted the array and got the range from greatest to smallest. My next plan was to loop through the sorted array and use a nested loop to do something like if (i % j === 0) if ( j < smallestMultiple) smallestMultiple = j;
Thanks, if i need to go back to earlier lessons and understand more of the basics to getting this I will do so, I felt the previous Primes challenge was a big jump from the challenges before that.
Your code so far
function smallestCommons(arr) {
arr.sort((a,b) => a -b);
let max = Math.max(...arr);
let min = Math.min(...arr);
let count = min + 1;
let numbers = arr.sort((a,b) => b-a)
}
console.log(smallestCommons([1, 5]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.
try googling algorithms to find smallest common multiples, you could find them on wikipedia or on maths focused places, then you just need to convert it to JavaScript - do not invent an already existing wheel
So after a good research of algorithms and quite a bit of guidance on Youtube, I have come up with this.
function smallestCommons(arr) {
arr.sort((a,b) => a -b);
let max = Math.max(...arr);
let min = Math.min(...arr);
let count = min + 1;
let numbers = arr.sort((a,b) => b-a)
let result = numbers [0];
for (let i = 1; i < numbers.length; i++) {
result = findLCM(result, numbers[i]);
}
return result;
}
function findLCM(x, y) {
var max;
max = (x > y) ? x:y;
while (true) {
if (max % x ===0 && max % y ===0) {
return max;
}
max ++
}
}
console.log(smallestCommons([1, 5]))
I’m still not getting the right answer though, any ideas?
I should of deleted the count variable, I dont think i need it anymore. The numbers array was to sort the code from greatest to smallest, Ive only just noticed that mistake that there is a gap between numbers and [0] I was intended to grab the first element of the array.
function smallestCommons(arr) {
arr.sort((a,b) => a -b);
let max = Math.max(...arr);
let min = Math.min(...arr);
let numbers = arr.sort((a,b) => b-a)
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
result = findLCM(result, numbers[i]);
}
return result;
}
function findLCM(x, y) {
var max;
max = (x > y) ? x:y;
while (true) {
if (max % x ===0 && max % y ===0) {
return max;
}
max ++
}
}
console.log(smallestCommons([1, 5]))
The lowest common multiples of each number in the array, Is this not a correct approach if i compare two numbers in the array using the findLCM function?
Thanks @ilenia, that was tough! now solved.
What would be the best way to refactor the start of the code that you mention above? here is my solution
function smallestCommons(arr) {
arr.sort((a,b) => a -b);
let max = Math.max(...arr);
let min = Math.min(...arr);
let count = min +1;
while (count < max) {
arr.push(count)
count++
}
let numbers = arr.sort((a,b) => b-a)
console.log(numbers)
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
result = findLCM(result, numbers[i]);
}
return result;
}
Does this get problem-solving get easier? the code makes complete sense to me and I understand every line. But i am really struggling with coming up with the logic myself to create a working solution and i was not making any progress trying to do it completely by myself.
for (let i = Math.min(...arr) +1; i < Math.max(...arr); i++) {
arr.push(i)
arr.sort((a,b) => b -a)
console.log(arr)
}
Tried to combine the sort and push together, but says not a function. Also deleted the numbers array as it was not in the correct scope after refactoring.