This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function staircase(n) { | |
for (let i = 1; i <= n; i++) { | |
console.log("#".repeat(i).padStart(n)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |