Posts

Showing posts from December, 2018

JS built-in methods have time complexities of their own

Time Complexity of JavaScript’s Built In Methods Best not to use them within a for loop as that would create an O(n^2) time complexity.

JS: analyze performance time for your code

Link: Console.time() & Console.timeEnd() console.time("Time this"); //start with console.time with a string argument then provide code after function sameFrequency(arg1, arg2){ if (arg1.toString().length !== arg2.toString().length) return false; let countObj = {}; for (let num of arg1.toString()) { if (countObj[num]) countObj[num]++; else countObj[num] = 1; } for (let item of arg2.toString()) { if (!countObj[item]) return false; else countObj[item]--; } for (let item in countObj) { if (countObj[item] > 0) return false; return true; } }; console.timeEnd("Time this"); //end with console.timeEnd with same string argument

CSS- how margins work

Didn't quite realize until now that an element's margin doesn't respect the margins of other elements next to it. Basically, I see them overlapping in Dev Tools. That explains why adjusting padding is not the same as adj margins when you're spacing things out.

Code Wars: Persistent Bugger (6 kyu)

Link to code challenge function persistence(num) { if (num.length 1) { let numArr = number.split("").map(item => Number(item)); let newNum = 1; for (let item of numArr) { newNum *= item; } number = newNum.toString(); count++; } return count; }

Code Wars: Mumbling (using JS repeat method)

https://www.codewars.com/kata/mumbling/train/javascript function accum(s) { let count = 0;   let storage = "";     for (let letter of s) {     storage += letter.toUpperCase() + letter.repeat(count) + "-";     count++;   }     return storage.substring(0, storage.length - 1); }

Code Wars: The Coupon Code

https://www.codewars.com/kata/the-coupon-code/train/javascript function checkCoupon(enteredCode, correctCode, currentDate, expirationDate){   let date = new Date(currentDate);   let expDate = new Date(expirationDate); //don't need  getTime()  actually since we have date object already or can do Date.parse(string)   if (date.getTime() - expDate.getTime() > 0) return false;      if (enteredCode !== correctCode) return false;   return true; }

for of vs for in loop

To loop over each item of a list: Arrays use: for...of Objects use: for...in

Code Wars: Jaden Casing Strings

https://www.codewars.com/kata/5390bac347d09b7da40006f6/solutions/javascript String.prototype.toJadenCase = function (str) {   let arr = this.split(' ');     for (i = 0; i < arr.length; i++) {     arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1);   }   return arr.join(' '); };

FCC- Project Euler: Problem 8: Largest product in a series

https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-8-largest-product-in-a-series (more efficient to try: use a pointer variable to keep track of where you are in the array while looping over it instead of using a nested for loop) function largestProductinaSeries(n) { // Good luck! let thousandDigits = [ 7 , 3 , 1 , 6 , 7 , 1 , 7 , 6 , 5 , 3 , 1 , 3 , 3 , 0 , 6 , 2 , 4 , 9 , 1 , 9 , 2 , 2 , 5 , 1 , 1 , 9 , 6 , 7 , 4 , 4 , 2 , 6 , 5 , 7 , 4 , 7 , 4 , 2 , 3 , 5 , 5 , 3 , 4 , 9 , 1 , 9 , 4 , 9 , 3 , 4 , 9 , 6 , 9 , 8 , 3 , 5 , 2 , 0 , 3 , 1 , 2 , 7 , 7 , 4 , 5 , 0 , 6 , 3 , 2 , 6 , 2 , 3 , 9 , 5 , 7 , 8 , 3 , 1 , 8 , 0 , 1 , 6 , 9 , 8 , 4 , 8 , 0 , 1 , 8 , 6 , 9 , 4 , 7 , 8 , 8 , 5 , 1 , 8 , 4 , 3 , 8 , 5 , 8 , 6 , 1 , 5 , 6 , 0 , 7 , 8 , 9 , 1 , 1 , 2 , 9 , 4 , 9 , 4 , 9 , 5 , 4 , 5 , 9 , 5 , 0 , 1 , 7 , 3 , 7 , 9 , 5 , 8 , 3 , 3 , 1 , 9 , 5 , 2 , 8 , 5 , 3 , 2 , 0 , 8 , 8 , 0 , 5 , 5 , 1 , 1 , 1 , 2 , 5 , 4 , 0 , 6 , 9 , 8 , 7 , 4 , 7 , 1 , 5 , 8 , 5 ,...

CSS- make bottom element fade into element above it

Instead of having elements divided by a straight line, make them fade into each other with bottom element having these settings: Top element also needs to have same overlay color (can mess around with the opacity to match both elements) .stats-sec { background-color : #2E2F3A ; opacity : .99 ; //so shadow blends in with the background color box-shadow : 0 -15 px 10 px #2E2F3A ; //so only top edge is blurred height : 100 px ; }

CSS- drop down menu displayed over content below it

Put position of menu to 'relative' and content's position to 'absolute' which removes element from normal flow of document.

Change icon/images look with Paint 3D

Edit image with color fill to change its color.

Coderbyte: Kaprekars Constant

https://coderbyte.com/information/Kaprekars%20Constant function KaprekarsConstant(num) {         let counter = 1;         function numDifference(entry) {       let result = (Number(entry.toString().split("").sort((a,b) => b - a).join("")) - Number(entry.toString().split("").sort((a,b) => a - b).join(""))).toString();       if (result.length < 4) {           for(let i = result.length; i < 4; i++) {               result = "0" + result;           }       }       return result;    //string     }         let finalNum = numDifference(num);  //string     console.log(numDifference(num))         while(numDifference(finalNum) !== finalNum) {         counter++;         ...

FCC- Project Euler: Problem 7: 10001st prime (now passes with more efficient code)

https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-7-10001st-prime function nthPrime(n) { let prime = [2]; let i = 3 ; //even numbers are not prime (except 2) let isPrime = true; while (prime.length < n) { for (let j = 0 ; prime[j] <= Math.ceil( Math.sqrt(i) ); j++) { if (i !== prime[j] && i % prime[j] == 0 ) { isPrime = false; break ; } } if (isPrime) prime.push(i); isPrime = true; i += 2; //even numbers are not prime } return prime[prime.length - 1]; } nthPrime( 10001 );

JS: hiding elements but still want it in normal flow

Instead of setting to 'display: none', make its opacity to 0.

prevent child element from receiving parent's hover effect

You want list to have hover effect but not the submenu items: < li >Company< img class = " arrow-down " src = " ./images/navigate-arrows.png " alt = " arrow down " > < div class = " submenu-items " > < div >About</ div > < div >Careers</ div > < div >News</ div > < div >Events</ div > < div >Blog</ div > </ div > </ li > Offset child element to change color to black (instead of gray on the list): .menu-items li : hover > .submenu-items { color : black ; }

event.stopPropagation() on menu icon click event handler

With a menu icon that displays a menu list on click and hides menu when clicked again, you don't want any other click event listeners responding to the click on the menu icon. To prevent this, you would need to add "event.stopPropagation()" to the menu icon event handler. Definition:  Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. This is especially needed when you have a click event listener on the document where whenever you click anywhere outside the menu list items, the menu list would close/hide. If there was no event.stopPropagation(), when you click on the menu icon, this listener on the document would respond and hide the menu list even though you are trying to display it. $ ( " .menu-items " ). hide (); //hide menu on page load $ ( " .menu " ). click ( function () { event . stopPropagation (); //so clicking menu icon does not trigger event...