This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Let's say that your age :) | |
| let age = 30; | |
| // This should never happen in real life... | |
| // You just can't divide number with string! | |
| age = age / '2'; | |
| // Ohh... Am I seeing 15 as a result? | |
| // WOW JS... Is it a miracle? Or just implicit coercion? | |
| console.log(age); // 15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // declare your variable - default type - number | |
| // Type is assigned to a value eg. 10 is a number (not variable level) | |
| let level = 10; | |
| // I change numeric value of level's value | |
| level = 15; | |
| // I change value type to string - explicit coercion | |
| level = String(level); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumPrimesInt(number) { | |
| function findPrimes(value) { | |
| // 1. Loop through defined range, (0 and 1 are not primes so skip them) | |
| for (let i = 2; i <= value; i++) { | |
| // 2. Those numers are not primes | |
| if (value % i === 0 && value != i) { | |
| return false; | |
| } | |
| } | |
| // 3. If the loop completed a number is prime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumPrimesBasic(number) { | |
| // 1. Create final sum and call function for finding primes | |
| const findPrimes = (maximum) => { | |
| // 2. Inside function that finds prime declare initial variables | |
| let filter = [], | |
| primes = []; | |
| // 3. Loop through number smaller than the argument | |
| for (let i = 2; i <= maximum; i++) { | |
| // 4. This array contains multiplications. If it's there a number can't be prime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Build an object with particular entities | |
| const htmlEntities = { | |
| '&': '&', | |
| '<': '<', | |
| '>': '>', | |
| '"': '"', | |
| "'": ''' | |
| }; | |
| function convertHTMLAdv(string, entities) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function convertHTMLInt(string) { | |
| // 1. Use build in replace method and RegExp to change characters | |
| return string | |
| .replace(/&/g, '&') | |
| .replace(/</g, '<') | |
| .replace(/>/g, '>') | |
| .replace(/"/g, '"') | |
| .replace(/'/g, '''); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Convert HTML characters | |
| function convertHTMLBasic(string) { | |
| // 1. Convert a string into array and check each element | |
| let array = string.split('').map((a) => { | |
| // 2. If an element match to defined HTML entity | |
| // replace it with text eqivalent | |
| switch (a) { | |
| case '<': | |
| a = '<'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Conver Arabic to Roman Numbers | |
| function romanConventerBasic(number) { | |
| // 1. Create array with roman numbers and arabic equivalents | |
| const decimalValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
| const romanValue = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']; | |
| // 2. New variable that will be romanized number | |
| let romanized = ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // All odd Fibbonaci's | |
| function sumOddFibsAdv(number) { | |
| // 1. Create array with the two first values from the sequence | |
| let fibs = [0, 1]; | |
| // 2. Create the number sequence till defined parameter | |
| for (let fib = 1; fib <= number; ) { | |
| fibs.push(fib); | |
| fib = fibs[fibs.length - 1] + fibs[fibs.length - 2]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // All odd Fibbonaci's | |
| function sumOddFibsBasic(number) { | |
| // 1. Create variables storing two latest values from the Fibbonaci numbers | |
| // and value of a final sum off only odd numbers | |
| let previousNum = 0; | |
| let currentNum = 1; | |
| let result = 0; | |
| // 2. While loop iterates as long as current value is smaller that function parameter | |
| while (currentNum <= number) { |