Posts

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