Tell us what’s happening:
Below is the only code that I can confidently write. Everything else is a seething mass of variables that seem to have little to no connection to the code, placeholders that are likewise, etc. etc.
I’ve read through Euclid’s Algorithm and I’m trying my darnest to convert that to JS, but I’m really not managing, so below I have a mix of code and comments and what I’d like is someone to point to what my logic is doing wrong. I realize there are serious errors in logic, but I’m not sure how to conceptualize or phrase HOW I’m going wrong. If some could help me with this, then I would appreciate it.
You can be blunt. It’s fine. I actually prefer bluntness. But just note that I am aware of my deficiencies as a coder, so you don’t need to state this particular fact.
Thank you!
Your code so far
function smallestCommons(arr) {
//sort array
arr = arr.sort((a,b)=> a-b);
//set up empty array and variables
let array = [];
let a, b, c, d;
//use for loop to push all elements in arr as well as all elements in between elements in arr into array
for(let i = arr[0]; i <= arr[arr.length-1]; i++){
array.push(i);
console.log(array)
}
//Set up second loop to iterate through array and provide values for variables that will complete Euclid's algorithm
//Euclid's algorithm key chart being the following: a= dividend; b=divisor; c=quotient; d=remainder
for(let j = array[array.length[0]]; j <= array[array.length-1]; j++){
//Variable a has element in array of j iteration and b assigned to element one greater than value of a variable
a = array[j];
b=array[j+1];
//c quotient of b/a
c = b/a;
//d is remainder of b/a
d = b%a;
//while a and b are truthy values, perform Euclid's Algorithm
while(a&&b){
a = b*c + d;
//if the remainder is 0 then return a, else break code and break
if(d === 0){
return a
} else if {
break;
}
}
}
}
smallestCommons([1,5]);