First off, my apologies if this is in the wrong area. I wasn’t sure where to put it since it’s not an actual fcc “project” and I don’t need help as I’ve completed the challenge. Just looking for some general feedback and wondering if I over complicated the challenge. This is by far the longest piece of code I’ve written for one of these challenges, I’m just proud I actually got it done. Any feedback would be greatly appreciated. Specifically, I tried to take a functional programming approach and define functions that did as little as possible to make it more modular if I ever needed to change anything. Clearly, some of the individual functions are a little long and do more than one thing but approaching the problem from this angle of writting functions to process each step little by little was the only way I could wrap my head around tackling this challenge.
// function to find LCM
function smallestCommons(arr) {
// determine all factors
var range = rangeFinder(arr);
// check if factors are prime and return multidimensional arr with only prime factors
var primeCheck = primeChecker(range);
// find the greatest occurence of prime factors per primeCheck element
var checker = check(primeCheck);
// multiple each prime factor by the number of times it occurs and multiple the products of each result together for LCM
var lcm = lowest(checker);
return lcm;
}
// find range of numbers
function rangeFinder(arr) {
var x = arr[0];
var y = arr[1];
var rangeArr = [];
if (x > y) {
for (var i = y; i <= x; i++) {
rangeArr.push(i);
}
} else {
for (var j = x; j <= y; j++) {
rangeArr.push(j);
}
}
return rangeArr;
}
// check if a number is prime
function isPrime(num) {
if (num % 1 === 0) {
var primeCount = 0;
for (var i = num; i > 0; i--) {
if (num % i === 0) {
primeCount++;
}
}
if (primeCount > 2) {
return false;
} else {
return true;
}
} else {
return false;
}
}
// run range array through prime checker
function primeChecker(arr) {
var primedArr = [];
for (var i = 0, length = arr.length; i < length; i++) {
primedArr.push(factorToPrime(arr[i]));
}
return primedArr;
}
// reduce non-prime factors to prime factors
function factorToPrime(num) {
if (num > 1) {
var divisor = 2;
var factorArr = [];
if (!isPrime(num)) {
while(!isPrime(num/divisor)) {
if ((num/divisor) % 1 !== 0) {
divisor++;
while(!isPrime(divisor)) {
divisor++;
}
} else {
factorArr.push(divisor);
num /= divisor;
divisor = 2;
}
}
factorArr.push(num / divisor, divisor);
} else if (isPrime(num)) {
return num;
}
return factorArr;
} else if (num === 1) {
return num;
}
}
// check which prime number occurs the most per factor
function check(arr) {
var countArr = [];
for (var i = 0, length = arr[arr.length - 1]; i < length; i++) {
countArr[i] = 0;
}
for (var i = 0, length = arr.length; i < length; i++) {
if (isPrime(arr[i])) {
countArr[arr[i] - 1] = 1;
} else {
for (var j = 0, length2 = arr[i].length; j < length2; j++) {
if (countInArray(arr[i], arr[i][j]) > countArr[arr[i][j] - 1]) {
countArr[arr[i][j] - 1] = countInArray(arr[i], arr[i][j]);
}
}
}
}
return countArr;
}
// counts how many times an element occurs in an array
// used to determine the greatest occurrence of each prime factor respective to the elements in primeCheck
function countInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
// multiply to find lcm
function lowest(arr) {
var lcm = 1;
for (var i = 0, length = arr.length; i < length; i++) {
if (arr[i] > 0) {
lcm *= exponential(i + 1, arr[i]);
}
}
return lcm;
}
// find the product for each prime factor
function exponential(a, b) {
var i = 0;
var product = 1;
while (i < b) {
product *= a;
i++;
}
return product;
}