Tell us what’s happening:
Describe your issue in detail here.
I have a monstrosity that I made that I spent over 15 hours working on. I tried googling, but I can’t find a way to make an algorithm that accesses the base numbers paired with the largest exponents. I want to figure out how to access the values I need in the object I made. I also commented out an array loop (pause comment) that’s also a viable path for the solution.
**Your code so far**
function smallestCommons(arr) {
let numArray = [];
let biggestNum = 0;
//build an array with all numbers between each range.
if (arr[0] < arr[1]) {
for (let i = arr[0]; i <= arr[1]; i++ ) {
numArray.push(i);
}
}
else if (arr[0] > arr[1]) {
for (let i = arr[0]; i >= arr[1]; i--) {
numArray.push(i);
}
}
//console.log(numArray); //check
//get the biggest number in the array to prepare for primeNumber Array.
biggestNum = Math.max(...numArray)
//console.log(biggestNum) //check
//make the primeNumber Array up to the biggestNumber
let primeArray = [];
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
for (let i = 2; i <= biggestNum; i++) {
if (isPrime(i))
primeArray.push(i);
}
//console.log(primeArray) //check
//building necessary arrays and extracting useful information into them.
let globalDividend = 0;
let divisorArray = [];
let improvedDivisorArray = [];
for (let i = 0; i < numArray.length; i++) {
for (let j = 0; j < primeArray.length; j++) {
divisorPusher(numArray[i], primeArray[j], i);
}
}
function divisorPusher(dividend, divisor, index) { //function with recursion, so I don't have to do repetitive if else statements
if (dividend % divisor === 0 && dividend != 1) {
divisorArray.push(divisor);
globalDividend = dividend / divisor;
if (globalDividend != 1) {
divisorPusher(globalDividend,divisor);
}
}
}
console.log(divisorArray) //check
let counter = 1;
let reInitialize = 0;
for (let i = 0; i < divisorArray.length; i++) { //build exponentArray by adding 1 for every matching element ahead + 1
if (divisorArray[i] === divisorArray[i + 1]) {
counter++;
reInitialize--;
} else {
improvedDivisorArray.push(divisorArray.slice((i + reInitialize), (i + reInitialize) + counter))
counter = 1;
reInitialize = 0;
}
}
console.log(improvedDivisorArray)
//console.log(improvedDivisorArray[0][0] == improvedDivisorArray[2][0]) //test
//console.log(improvedDivisorArray[0][1]) //Undefined //test
/*
let uniques = [];
let test = [];
for (let i = 0; i < improvedDivisorArray.length; i++) {
let value = improvedDivisorArray[i];
let found = true;
for (let j = 0; j < uniques.length; j++) {
test = uniques[j];
if (value[0] === test[0] && value.length <= test.length) {
found = false;
break;
}
}
if (found) {
uniques.push(value);
}
}
console.log(uniques)*/ //Pausing this idea.
//Object idea
var divisorObj = Object.assign({},improvedDivisorArray);
console.log(divisorObj); //check
//divisorObj[0] = {"base": divisorObj[0][0], "exponent": divisorObj[0].length}; //test
for (let i = 0; i < improvedDivisorArray.length; i++) {
divisorObj[i] = {"base": divisorObj[i][0], "exponent": divisorObj[i].length};
}
console.log(divisorObj); //check
const asArray = Object.entries(divisorObj);
const filtered = asArray.filter(([key, value]) => value == divisorObj[1] );
const just2 = Object.fromEntries(filtered);
console.log(just2);
}
console.log(smallestCommons([2, 10]));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple
On the console output I was able to get a table that has the solution for [2,10]
That is return 2**3 * 3**2 * 5 * 7.
I’m going to look into the getter and setter suggestion, looks promising. I just want to figure out the syntax to reduce my code so that the smaller prime factors are removed.
You don’t need fancy object methods… You literally need to keep track of the maximum number of times each prime factor occurs. A basic object can do that.
For example, the LCM of all numbern from 2 to 10 has to have the prime factor 2 to the power of 3 because 8 has the prime factor of 2 to the power of 3.
Then you just multiply all of the powers of all of the primes you find.
If you don’t remember how to loop over numbers, you could loop over the array of all keys
If you get this approach working, I would look at the solutions. There is a prime factoring sample solution that is tidier that what you are writing.
After adding in another 5 hours of staring at the screen, I finally cracked the puzzle. I’m going to look at what the hint answer is and I should probably refractor the code. What my take away from this, is that I need to find a way to spend less time on a problem. Maybe it’s worth it just looking at the answer than spending hours solving many small problems until the big problem is solved.
yeah, when I am desperately stuck by the way - I don’t look up solutions - I am moving on to other problem.
Getting back to stuff where I am stuck from time to time.
Sometimes you just need to switch your brain to some other stuff for some time.
I was going to ask about this, curious myself.
I did not see any content which should be flagged in mentioned post.
The theory: maybe someone flagged it accidentally.
Sometimes I am ending up accidentally clicking flag button instead of the bookmark button, they are placed right to each other
You got it! Yes the flag was made to the moderator team because of the posted solution.
We encourage everyone on the forum to help each other with hints and tips, but not with solutions, as that makes the process of learning not as effective. (When you struggle to complete the exercise, your brain forms new neural pathways that support your future learning. But that does not happen when we simply copy and paste something we found online).