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
| 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
| // 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 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
| 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
| // 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
| // 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
| // Let's say that bank account | |
| let money = 1E6; | |
| // Your hard work pays off | |
| money = money + Number('.6E6'); | |
| // That looks better :) | |
| console.log(money); // 1 600 000 |
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
| // Non-coercive / strict comparison | |
| if ( 5 === 5 ) { | |
| console.log('No coercion please!'); | |
| } |
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 boots | |
| // Value assignemnt anti-pattern | |
| if (boots = 'green') { | |
| console.log(boots); | |
| } |