Skip to content

Instantly share code, notes, and snippets.

@bartcis
bartcis / algo_mid_four_1.js
Created May 27, 2019 15:03
HTML entities converter - solution 1
// 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 = '&lt;';
@bartcis
bartcis / algo_mid_four_2.js
Created May 27, 2019 15:05
HTML entities converter - solution 2
function convertHTMLInt(string) {
// 1. Use build in replace method and RegExp to change characters
return string
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
@bartcis
bartcis / algo_mid_four_3.js
Last active July 31, 2019 13:32
HTML entities converter - solution 3
// 1. Build an object with particular entities
const htmlEntities = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&apos;'
};
function convertHTMLAdv(string, entities) {
@bartcis
bartcis / algo_mid_five_1.js
Last active July 31, 2019 14:20
Find sum of all prime numbers within given range
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
@bartcis
bartcis / algo_mid_five_2.js
Created May 27, 2019 15:10
Find sum of all prime numbers within given range - solution 2
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
@bartcis
bartcis / value_as_type.js
Created June 3, 2019 14:31
JS value as type simple example
// 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);
@bartcis
bartcis / implicit_coercion.js
Last active June 3, 2019 16:37
Simple example of implicit coercion in JS
// 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
@bartcis
bartcis / explicit_coercion.js
Created June 3, 2019 15:32
Simple example of explicit coercion in JS
// 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
@bartcis
bartcis / strict_comaprison.js
Created June 3, 2019 16:39
Simple example of non-coercive comparison
// Non-coercive / strict comparison
if ( 5 === 5 ) {
console.log('No coercion please!');
}
@bartcis
bartcis / assignment_anti_pattern1.js
Created June 3, 2019 16:52
How not to assign values in JS
let boots
// Value assignemnt anti-pattern
if (boots = 'green') {
console.log(boots);
}