Posts

HackerRank: Jumping on the Clouds (Easy) [Javascript]

Link to code challenge function jumpingOnClouds(c) { let jumps = 0; let cloud = 0; while (cloud

HackerRank: Minimum Distances (Easy)

Link to code challenge function minimumDistances(a) { let obj = {}; let shortest = Infinity; for (let i = 0; i

HackerRank: Equalize the Array (Easy)

Link to code challenge function equalizeArray(arr) { let count = {}; let greatest = 1; for (let num of arr) { if (count.hasOwnProperty(num)) { count[num]++; if (count[num] > greatest) greatest = count[num]; } else count[num] = 1; } return arr.length - greatest; }

HackerRank: Cut the sticks (Easy)

Link to code challenge function cutTheSticks(arr) { let sticksLeft = []; while (arr.length > 0) { sticksLeft.push(arr.length); arr.sort((a, b) => b - a); let lowest = arr[arr.length - 1]; for (let i = arr.length - 1; i >= 0; i--) { if (arr[i] === lowest) arr.splice(i, 1); else arr[i] -= lowest; } } return sticksLeft; }

HackerRank: Designer PDF Viewer (Easy)

Link to code challenge function designerPdfViewer(h, word) { let max = 0; for (let i = 0; i max) max = h[numEq]; } return max * word.length; }

HackerRank: Beautiful Days at the Movies (Easy)

Link to code challenge function beautifulDays(i, j, k) { let count = 0; for (let day = i; day

HackerRank: Counting Valleys (Easy)

Link to code challenge function countingValleys(n, s) { let level = [0]; let count = 0; for (let step of s) { if (step === "U") { level.push(level[level.length - 1] + 1); if (level[level.length - 2]

HackerRank: Sock Merchant (Easy)

Link to code challenge function sockMerchant(n, ar) { let pairs = 0; let sum = 1; let arr = ar.sort(); for (let i = 1; i 1) pairs += Math.floor(sum / 2); sum = 1; } } return pairs; }

HackerRank: Migratory Birds (Easy)

Link to code challenge Naive approach. function migratoryBirds(arr) { let num = Infinity; let count = 0; let largestCount = 0; let newArr = arr.sort(); for (let i = 0; i largestCount) { largestCount = count; num = newArr[i - 1]; } //DON'T need this else if statement since newArr[i - 1] will always be greater than num as the arr is in numerical order else if (count === largestCount) { if (newArr[i - 1]

HackerRank: Birthday Chocolate (easy)

Link to code challenge function birthday(s, d, m) { let count = 0; let pointer = 0; while (pointer

HackerRank- Kangaroo (easy)

Link to code challenge Needed assistance on 2nd formula instead of doing an iteration. You first find the difference between the starting points of the two kangaroos. Next, you find the difference between the speed at which each kangaroo travels. This difference is how much the distance between the two kangaroos is decreasing by at each "iteration". Therefore, the difference in speed has to be a factor of the difference in their starting points in order for both kangaroos to eventually be on the same points. If the difference in distance is 7 but at each iteration the distance is reduced by 2, they will never be on the same point (7 / 2 !== 0). function kangaroo(x1, v1, x2, v2) { let pos1 = x1; let pos2 = x2; if ( (x1 > x2 && v1 > v2) || (x1

HackerRank- Mini-Max Sum (easy)

Link to code challenge function miniMaxSum(arr) { let newArr = arr.sort((a, b) => a - b); let lowest = newArr.slice(0, newArr.length - 1).reduce((a, b) => a + b); let highest = newArr.slice(1).reduce((a,b) => a + b); console.log(lowest, highest); }

Hacker Rank- Staircase (Easy)

Link to code challenge function staircase(n) { let count = n - 1; for (let i = "#"; i.length Without creating a new variable: function staircase(n) { for (let i = "#"; i.length

Hacker Rank- Diagonal Difference (Easy)

Link to code challenge function diagonalDifference(arr) { console.log(arr); let sum1 = 0; let sum2 = 0; let j = 0; let k = arr.length - 1; for (let i = 0; i

Code Wars: Rot13 (5 kyu)

Link to code challenge function rot13(message){ let newString = ""; for (let char of message) { if ( char.match(/[a-z]/i) === null ) newString += char; else if ( char.charCodeAt() > 77 && char.charCodeAt() 109 && char.charCodeAt()

Code Wars: Fibonacci, Tribonacci and friends (6kyu)

Link to code challenge function Xbonacci(signature,n){ if (!signature.length) return []; let arr = [...signature.slice(0, n)]; const sumLength = signature.length; while (arr.length a + b); arr.push(sum); } return arr; }

Code Wars: Consonant value (6kyu)

Link to code challenge function solve(s) { let sum = 0; let prevSum = 0; let regex = /[^aeiou]/; for (let letter of s) { if ( regex.test(letter) ) { const letterValue = letter.charCodeAt() - 96; sum += letterValue; } else if ( !regex.test(letter) ) { if (sum > prevSum) { prevSum = sum; sum = 0; } else sum = 0; } } return Math.max(sum, prevSum); };

check every item in loop before returning

Instead of an else statement inside for loop, put that outside of loop if loop does not return. for (let i = 0; i

Code Wars: The Shell Game (6 kyu)

Link to code challenge Code efficiency: Checks each nested array within array. find_the_ball=function(start,swaps){ if (swaps === []) return start; let position = start; for (let item of swaps) { if (item[0] === position) position = item[1]; else if (item[1] === position) position = item[0]; } return position; }

CSS: have sibling elements match each others height

I.e. When you have a list of items displayed and layout using flexbox. 1. Parent container element: .flex-container { display: flex; justify-content: center; flex-wrap: wrap; } 2. Each childen element: .flex-container section { /*so each element same height as the others*/ display: flex; }