Skip to content

Instantly share code, notes, and snippets.

View apolopena's full-sized avatar
💭
We live to learn!

Apolo Pena apolopena

💭
We live to learn!
View GitHub Profile
@apolopena
apolopena / JSPlusMinusProblemSolved.js
Created January 18, 2019 06:15
Javascript: answer to the plusminus interview questions
// not written by me, I just put it here to try to understand the question, lol
function plusMinus(arr) {
let pos = 0, neg = 0 , zero = 0 , length = arr.length
arr.forEach( n => {
if( n > 0 )
pos++
else if( n < 0 )
neg++
else
zero++
@apolopena
apolopena / JSStringStairCase.js
Created January 19, 2019 00:28
JavaScript: output a right-justified 'staircase' of strings SLICK SOLUTION
function staircase(n) {
for (let i = 1; i <= n; i++) {
console.log("#".repeat(i).padStart(n));
}
}
@apolopena
apolopena / JSSStairCaseCLUNKY.js
Created January 19, 2019 01:06
JavaScript: output a right-justified 'staircase' of strings CLUNKY SOLUTION
function staircase(n) {
let stairs = [];
for (let i = 0; i < n; i++) {
stairs.push(new Array(n).fill(' ')); // build a square 2d array (N high by N wide)
}
stairs.forEach((row, rIndex) => {
row[rIndex] = '#'; // set the markers for the steps (#) left justified
row.reverse(); // reverse the array to make the staircase right justified
// now edit the characters in the 2d array so they properly represent the staircase
if(rIndex === 0) { // first row is always correct so do nothing
@apolopena
apolopena / JSMinMaxSumOfArray.js
Created January 20, 2019 07:03
JavaScript: print the min and max sum of an array of numbers:
function miniMaxSum(arr) {
let minArr, maxArr;
minArr = [...arr];
maxArr = [...arr];
let sumArray = function (arr) {
return arr.reduce((sum, num) => { return sum += num; });
}
minArr.splice(minArr.indexOf(Math.max(...minArr)), 1);
maxArr.splice(maxArr.indexOf(Math.min(...minArr)), 1);
console.log(`${sumArray(minArr)} ${sumArray(maxArr)}`);
@apolopena
apolopena / JSSumHighestValuesOfArray.js
Last active January 20, 2019 20:06
JavaScript: Sum only the numbers of the highest value in an array (a.ka Birthday cake candles height problem)
// a two liner using Array.filter
function birthdayCakeCandles(ar) {
const tallest = Math.max(...ar);
return ar.filter(candle => candle === tallest).length;
}
// or a 5-liner using a for loop
function birthdayCakeCandlesFor(ar) {
const tallest = Math.max(...ar);
let count = 0;
for (let i = 0; i < ar.length; i++) {
@apolopena
apolopena / JSConvert12to24Time.js
Last active May 2, 2023 12:24
JavaScript: convert 12 hours time string to 24 hour time string
// NOTE: time string must look like this 07:05:45PM or this 07:05:45AM and account for 12:00:00AM and convert 12:00:00pm to 12:00:00
function timeConversion(s) {
const ampm = s.slice(-2);
const hours = Number(s.slice(0, 2));
let time = s.slice(0, -2);
if (ampm === 'AM') {
if (hours === 12) { // 12am edge-case
return time.replace(s.slice(0, 2), '00');
}
return time;
@apolopena
apolopena / JSRoundinGradesProblemSolved.js
Created January 21, 2019 08:07
JavaScript: round grades in mutiples of 5 if the remainder is less than 3 and the grade >= 38
// my solution
function gradingStudents(grades) {
return grades.map((grade, i) => {
if (grade >= 38) {
let r = grade % 5;
if (r === 0 || r < 3) {
return grade;
}
return grade + 5 - (grade % 5);
}
@apolopena
apolopena / JSHRApplesAndOrangesFallingSolved.js
Created January 21, 2019 20:37
JavaScript: HackerRank Apples and Oranges Tree falling by the house problem solved
// my solution using easy to refactor methods (in case you wanted to either share logic or compare new types of fruit)
function countApplesAndOranges(s, t, a, b, apples, oranges) {
const fLoc = function (treeLoc, arr2d) {
return arr2d.map(fruitLoc => (treeLoc + fruitLoc));
}
const fRange = function (s, t, arr2d) {
let a, b;
a = 0; b = 0;
arr2d.forEach((f, i) => {
if (i === 0) { // apple count
@apolopena
apolopena / JSFibonacciSeqIterative.js
Created January 24, 2019 03:14
JavaScript: Fibonacci #'s Iteravely by value or by set
function fib(n, isSeq) {
let seq = [];
let Fn = 0, i = 2;
let Fn_1 = 1, Fn_2 = 1;
while( i < n) {
Fn = Fn_1 + Fn_2; // Fibonacci rule Fn = F(n - 1) + F(n - 2);
var prevFn_1 = Fn_1;
Fn_1 = Fn;
Fn_2 = prevFn_1;
seq.push(Fn);
@apolopena
apolopena / JSPhiSequence.js
Created January 24, 2019 04:17
JavaScript: calculate a set of golden ratio's (a.ka. PHI)
function phi(n) { // do not surpass n >= 79 due to JavaScript's 32 bit integer limitation regarding decimal precision
let ratios = [1, 1, 2], i = 2;
let fs = fib(n, true);
while (i < n - 1) {
ratios.push((fs[i + 1] / fs[i]).toFixed(3));
i++;
}
return ratios;
}
function fib(n, isSeq) {// accuracy is lost after n > 79