Skip to content

Instantly share code, notes, and snippets.

View niktariy's full-sized avatar
🎯
Focusing

Veronika niktariy

🎯
Focusing
View GitHub Profile
@niktariy
niktariy / index.md
Created July 22, 2022 07:52
Подборка сайтов с бесплатными иллюстрациями
@niktariy
niktariy / index.js
Created January 8, 2020 11:50
Arrow function with ...rest parameter
const average = (...args) => {
if (args.length == 0) return 0;
const sumReduceFn = function (a, b) { return a + Number(b) };
return args.reduce(sumReduceFn, 0) / args.length;
}
@niktariy
niktariy / index.js
Created January 8, 2020 11:47
Regular function that return the result of an immediately invoked nested arrow function
function average() {
return (() => {
const length = arguments.length;
if (length == 0) return 0;
const numbers = Array.prototype.slice.call(arguments);
const sumReduceFn = function (a, b) { return a + Number(b) };
return numbers.reduce(sumReduceFn, 0) / length;
@niktariy
niktariy / index.js
Created September 25, 2019 17:30
Function overloading with arrow functions
const average = () => {
const length = arguments.length;
if (length == 0) return 0;
const numbers = Array.prototype.slice.call(arguments);
const sumReduceFn = function (a, b) { return a + Number(b) };
return numbers.reduce(sumReduceFn, 0) / length;
}
@niktariy
niktariy / results.js
Created September 25, 2019 17:21
Function overloading results
average(); // 0
average('3o', 4, 5); // NaN
average('1', 2, '3', 4, '5', 6, 7, 8, 9, 10); // 5.5
average(1.75, 2.25, 3.5, 4.125, 5.875); // 3.5
@niktariy
niktariy / index.js
Created September 25, 2019 17:21
Function overloading
function average() {
// the number of arguments passed
const length = arguments.length;
if (length == 0) return 0;
// convert the arguments to a proper array of numbers
const numbers = Array.prototype.slice.call(arguments);
// a reducer function to sum up array items
@niktariy
niktariy / index.js
Created September 22, 2019 16:12
Behavior of duplicate parameters in arrow functions
// Always throws a syntax error
const logParams = (first, second, first) => {
console.log(first, second);
}
@niktariy
niktariy / index.js
Created September 22, 2019 16:07
Behavior of duplicated named parameters in strict mode
// Throws an error because of duplicate parameters (Strict mode)
function logParams (first, second, first) {
"use strict";
console.log(first, second);
}
@niktariy
niktariy / index.js
Created September 22, 2019 15:49
Behavior of duplicated named parameters in non-strict mode
function logParams (first, second, first) {
console.log(first, second);
}
// first => 'Hello'
// second => 'World'
// first => '!!!'
logParams('Hello', 'World', '!!!'); // "!!!" "World"
// first => { o: 3 }
@niktariy
niktariy / index.js
Created September 22, 2019 15:24
Example of function with named parameters
function logParams (first, second, third) {
console.log(first, second, third);
}
// first => 'Hello'
// second => 'World'
// third => '!!!'
logParams('Hello', 'World', '!!!'); // "Hello" "World" "!!!"
// first => { o: 3 }