Recursion
Definition Recursion is a programming concept, which happens when a function calls itself. It is used in a mathematical problem solving and a logical solution.
This function is calling itself, and it is a dangerous as it is calling itself indefinitely. Therefore, we need to define the exiting condition, which we call a ‘base case’. Every recursive function needs to have it to exit a function properly when a condition is met.function sayHeeya() { console.log("Hi heeya"); sayHeeya(); } sayHeeya();
function sayHeeya(count) { if (count == 0){ return; } console.log("Hi heeya"); sayHeeya(count - 1); } sayHeeya(10);
Each one of the functions copies will have a new set of internal variables called a stack. Now without a base case JavaScript will continue to ask for new variables until it either runs out of memory or the browser runs into an internal limit. Now when the computer runs out of memory that’s called a stack overflow. (Villalobos)
Recursion is very useful when the current call relies on the result of the previous call.function twoToThePowerOf(number) { if (number == 0) { return 1; } else { return 2 * twoToThePowerOf(number - 1); } } console.log(twoToThePowerOf(2));
Now, let’s take a look at the typical interview question for recursion. Calculating Fibonacci number. [What is the Fibonacci number?]2 ^ 5 = 2 * (2 ^ 4) = 2 * 2 * (2 ^ 3) = 2 * 2 * 2 * (2 ^ 2) = 2 * 2 * 2 * 2 * (2 ^ 1) = 2 * 2 * 2 * 2 * 2 * (2 ^ 0) = 2 * 2 * 2 * 2 * 2 * 1
Reference When would you use recursion? Mastering Web developer Interview Code – Ray Villalobos, Lynda.com LINK]]>function fibonacci(number) { if (number == 0 || number == 1) { return 1; } else { return fibonacci(number - 1) + fibonacci(number - 2); } } console.log(fibonacci(9)); // 55
A pseudo class VS a pseudo element
Definition A CSS pseudo-class: We use it to define a state or a property of the element. It is a way of selecting an “existing HTML element”. A CSS pseudo-element: We use it to style a specified element. It does not select an element, but it creates a virtual element. Let’s take a look at what these definitions mean. A pseudo-class Again, it is related to an existing element, and we use a single colon character in CSS.
:link, :visited, :hover, :active :focus, :first-child, :nth-child
A pseudo-element It targets the virtual element and style it, and we use double colons to identify pseudo elements. (Sometimes we just use a single colon for a pseudo-element because some older browsers don’t support double colons.)::before, ::after, ::first-letter
//first-letter selects something that is not an element //but the first letter of the paragraph p::first-line { color: #fff; }
Reference What’s the difference between a pseudo class and a pseudo element? Mastering Web developer Interview Code – Ray Villalobos, Lynda.com LINK]]>Immutability in JavaScript
Definition Immutability is an important concept, and it means that an element is unchangeable. But the meaning of immutability in JavaScript is a little bit different than other languages. What’s not immutability in JavaScript ‘const’ is not about immutability! We have a new keyword called const in JavaScript version 6, and it seems like making an unchangeable variable like what some other languages do. However, we can change the values of an element in an array using the following notation.
This works because arrays are assigned their reference, not to values. So, the elements in myArr can be modified even with the keyword ‘const’, and this happens a lot in JavaScript. Therefore, we should think ‘immutability’ as a concept, and we need to try to achieve this goal as we write our programs. Why is immutability important in JavaScript? Consider that we have a mutable array, and use the forEach method to add one to each element.const myArr = [1,2,3]; myArr[0]=6;
//MUTABLE var myArr = [5,6,7]; myArr.forEach(function(item, index) { myArr[index] = item + 1; }); console.log(myArr); // [6, 7, 8]
The map function is an immutable function that does not modify the original array. But it returns a new array that we can use in a variable. It is much safer way as it protect the original information. So the important concept with 'immutability' is that we need to create an immutable function to protect the original information (values) of the data! How does immutability work in JavaScript? Mastering Web developer Interview Code – Ray Villalobos, Lynda.com LINK]]>var myArr = [5, 6, 7]; function increase(item) { return item +1; } var modArr = myArr.map(increase); console.log(modArr)
Map( ) function
Definition The map() function The benefit of using the map() is immutability. Therefore it avoids changing the original array, while ‘for’ and ‘forEach’ transform the original array. Example 1 Let’s take an array and assign it to a new array.
The map function returns an array that has been modified with the callback. It takes each one of items, adds 1 to it, and put the results of each one of those modifications into the new array. The original array stays the same. Example 2 Let’s make the previous callback function into a separate function.var myArr = [5,6,7] var newArr = myArr.map(function(item) { return item + 1; }); console.log(newArr); //Result: [6, 7, 8]
Example 3 The map() is returning a new array, without transforming the original data. We can chain some transformation together.var myArray = [5, 6, 7]; function inc(item) { return item + 1; } var modArr = myArr.map(inc); console.log(modArr); //Result: [6, 7, 8]
var myArr = [5,6,7] function inc(item){ return item + 1; } function square(item) { return item * item; } var modArr = myArray.map(inc).map(square); console.log(modArr); // Result: [36, 49, 64]
Ems VS Rems
Definition Ems and Rems are units of measurements in CSS and we use them when working with fonts. Em unit is the relative size of its nearest or direct parent. Rem stands for “Root EM”, and this unit allows you to set an em on the HTML(root) font-size. Note Every browser has a default font and that is 16 pixels in most cases, and Rems are always relative to the root font size. Here, I will set the font size at 18 pixels. Example
And now, I will set the default font size of the root to 10px so that we can easily calculate related font sizes. So if we set the font size of in html to 62.5%, the font size becomes 10px, which make the after calculation easy. If we want to se the body font-size to 16px, we can give 1.6 rem to the body.html { font-family: sans-serif; } body { font-size: 18px; }
Next, let’s use Ems with Rems!html{ font-family: sans-serif; font-size: 62.5%; body { font-size: 1.6rem; }
.description{ font-size: 1rem; } .description-text{ font-size: 2em; } .workedfor{ font-size: 1.5em; }