Posts

Showing posts from January, 2019

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; }

Code Wars: longest_palindrome (6 kyu)

Link to code challenge Find the length of the longest substring in the given string s that is the same in reverse. As an example, if the input was “I like racecars that go fast”, the substring (racecar) length would be 7. If the length of the input string is 0, the return value must be 0. //Loop thru string and find longest substring that is also a palindrome //Efficiency- start with longest length first (each "outer" loop still looping to shorter //strings but done prior to "inner" loops which have longer strings- can be more efficient?) longestPalindrome=function(s){ if (s === "") return 0; else if (s.length === 1) return 1; let mainPointer = s.length; let subPointer = 0; let largest = 0; while (subPointer = 0) { //refactored- instead of pushing to new array let newString = s.slice(subPointer, mainPointer); //can this be refactored by comparing the two endpoints of the string but longer code? if ( newStr...

Code Wars: Data Reverse (6 kyu)

Link to code challenge Code efficiency: Loops through string once (from the end) and with one new variable. String splicing- consider its efficiency? Try not to manipulate data when looping for efficiency. function dataReverse(data) { let arr = []; while (data.length) { arr.push(...data.splice(data.length - 8, data.length)); } return arr; }

Udemy JS Algorithms & Data Structures Masterclass- Section 6 Exercise 9 (1st try)

Sliding Window - findLongestSubstring Write a function called findLongestSubstring, which accepts a string and returns the length of the longest substring with all distinct characters. My code efficiency: Reset pointer to next element to start count from there when you find a duplicate letter in the string. //start search from first element of string //each element after must not have been a previous element //if duplicate, break out of loop. Move pointer up 1. //end while loop when pointer is >= string's length function findLongestSubstring(input){ let pointer = 0; let point2 = 0; let count = 0; let arr = []; if (!input.length) return 0; while(pointer count) count = arr.length; } else if (arr.length === "" || arr.length count) { count = arr.length; arr = []; pointer = point2++; } pointer++; } return count; }

Udemy JS Algorithms & Data Structures Masterclass- Section 6 Exercise 8 (1st try)

Sliding Window - minSubArrayLen Write a function called minSubArrayLen which accepts two parameters - an array of positive integers and a positive integer. This function should return the minimal length of a contiguous subarray of which the sum is greater than or equal to the integer passed to the function. If there isn't one, return 0 instead. My code efficiency: starts with shortest lengths first (then increments length by 1 each loop) so returns first one greater than or equal to integer. //loop thru array once and check if any element greater than or equal to integer- return 1 //loop thru array starting from each pointer, iterate to length of 2. If any summed to be greater than or equal to integer, return 2 //From each pointer, iterate to length of 3. If any summed to be greater or equal to integer, return 3. //... function minSubArrayLen(arr, num) { let pointer = 0; let currLength = 1; for (let i = pointer; i = num) return currLength; } curr...

Udemy JS Algorithms & Data Structures Masterclass- Section 6 Exercise 5

Multiple Pointers - isSubsequence Write a function called isSubsequence which takes in two strings and checks whether the characters in the first string form a subsequence of the characters in the second string. In other words, the function should check whether the characters in the first string appear somewhere in the second string, without their order changing. function isSubsequence(string1, string2) { //find letter in second string //each letter found after should come after the previous letter //each letter after should be searched after position of previous letter let pointer = 0; let pointer2 = 0; let match = 0; while (pointer