How may I change he filters in order to get the LCM of the arrays?
I am trying to get complet these two: smallestCommons([2, 10]) should return 2520.
smallestCommons([1, 13]) should return 360360.
function smallestCommons(arr) {
let a=[];
for(let i=arr[0];i<=arr[1];i++){
for(let j=arr[0];j<=i;j++){
if(i%j==0){
a.push(j)
}
}
}
let evenArr=a.filter(e=>e%2==0)
let oddArr=a.filter(e=>e%2!=0)
let multipliedArr=oddArr.reduce((a,b)=>a*b)
console.log(oddArr)
let maxEvenNum=Math.max(...evenArr)
//console.log(multipliedArr)
//console.log(maxEvenNum)
let b=multipliedArr*maxEvenNum
//console.log(b)
return b;
}
smallestCommons([1,13]);
1 I need to get the range of numbers 2 I need to get the multiples of every number in that range 3 I need to know how to employ them to know the smallest number, which is multiple of every number in that range.
1 Get the highest number of the range.
2 Verify if it is divisible by every number in the range.
3 If it is not, add 1.
4 Repeat until you get the number, which is divisible by every number in the array.
I do not understand the reason why this is not working.
function smallestCommons(...arr) {
let counter=arr[0][1];
let a=[];
for(let i=arr[0][0];i<=counter;i++){
if(arr[0][1]%i==0){
}else{
a.push(i)
}
}
console.log(a)
while(a.length==0){
counter=counter+1
}
console.log(a)
}
smallestCommons([1,5]);
for (let i = arr[0][0]; i <= counter; i++) {
...
}
This is saying that you start at arr[0][0] and check values of i until i <= counter === arr[0][1]. If these two arguments are in order, then your start at the smallest, and then execute the loop body.
1To get the highest number.
2 To get the lowest number.
3 To get the range of numbers.
4 To divide the highest number by every number of the range.
5 if there are no multiple number, repeat until you do not get any no multiple number.
6 Return the number where you do not have any multiple number.
I do not understand the reason why this is not working.
function smallestCommons(arr) {
let maxNum=Math.max(...arr)
let minNum=Math.min(...arr)
let n=maxNum
let noMultArr=[];
for(let i=minNum;i<=maxNum;i++){
while(n%i!=0){
console.log(i)
n=n+1
}
}
return n;
}
smallestCommons([1,5]);