Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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_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_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_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_three.js
Last active December 18, 2019 12:11
Convert Arabic numbers to Roman notation
// 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 = '';
@bartcis
bartcis / algo_mid_two_2.js
Created May 27, 2019 14:59
Sum up all odd Fibbonaci numbers - solution 2
// 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];
}
@bartcis
bartcis / algo_mid_two_1.js
Created May 27, 2019 14:57
Sum up all odd Fibbonaci numbers - solution 1
// 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) {