Skip to content

Instantly share code, notes, and snippets.

@jurnalanas
Last active March 3, 2020 12:57
Show Gist options
  • Save jurnalanas/1d42f0fbd9a0b710d0fca1ff171c8f0f to your computer and use it in GitHub Desktop.
Save jurnalanas/1d42f0fbd9a0b710d0fca1ff171c8f0f to your computer and use it in GitHub Desktop.
// Second Great Low
function secondGreatLow(arr) {
let result = [];
let sortedArr = arr.sort((a,b) => a-b);
// turn into Set data structure first, then convert it again to array
sortedArr = [...new Set(sortedArr)];
const length = sortedArr.length;
// need more info on how many numbers minimum that must be returned
if (length == 2) {
result.push(sortedArr[length-2])
} else {
result.push(sortedArr[1])
result.push(sortedArr[length-2])
}
return result.join(' ');
}
// Multiplicative Persistence
function MultiplicativePersistence(num) {
let count = 0;
let currentNum = num;
function multiply(arr){
const result = arr.reduce((a,b) => a*b);
return result;
}
while(currentNum.toString().length > 1) {
let tempNum = currentNum.toString().split("");
currentNum = multiply(tempNum);
count +=1;
}
return count;
}
// Mean Mode
function MeanMode(arr) {
let result = 0;
const meanValue = parseInt(mean(arr));
const modeValue = parseInt(mode(arr));
if (meanValue === modeValue) {
result = 1;
}
function mean(numbers) {
let total = 0,
i;
for (i = 0; i < numbers.length; i += 1) {
total += numbers[i];
}
return total / numbers.length;
}
function mode(numbers) {
let modes = [],
count = [],
i, number, maxIndex = 0;
for (i = 0; i < numbers.length; i += 1) {
number = numbers[i];
count[number] = (count[number] || 0) + 1;
if (count[number] > maxIndex) {
maxIndex = count[number];
}
}
for (i in count)
if (count.hasOwnProperty(i)) {
if (count[i] === maxIndex) {
modes.push(Number(i));
}
}
return modes;
}
return result;
}
console.log("== Second Great Low ==")
const testArr = [7,7,12,98,106]
const testArr2 = [7,12]
secondGreatLow(testArr)
secondGreatLow(testArr2)
console.log("== Multiplicative Persistence ==")
MultiplicativePersistence(39)
MultiplicativePersistence(12)
console.log("== Mean Mode ==")
MeanMode([1,2,3,3]);
MeanMode([5,3,3,3,1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment