Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / curried-function-converter.js
Created May 15, 2021 21:41
JS Curried Function (Converter)
const converterOld = (toUnit, factor, offset, input) => {
const converterOffset = offset || 0;
return [((converterOffset + input) * factor).toFixed(2), toUnit].join(' ');
};
const convert10MilesToKm = converterOld('km', 1.60936, undefined, 10);
const convert20MilesToKm = converterOld('km', 1.60936, undefined, 20);
const convert30MilesToKm = converterOld('km', 1.60936, undefined, 30);
console.log('convert10MilesToKm : ', convert10MilesToKm); // "16.09 km"
@helabenkhalfallah
helabenkhalfallah / js-curried-functions-1.js
Last active May 15, 2021 21:30
JS Curried functions (1)
const converterOld = (toUnit, factor, offset, input) => {
const converterOffset = offset || 0;
return [((converterOffset + input) * factor).toFixed(2), toUnit].join(' ');
};
const convert10MilesToKm = converterOld('km', 1.60936, undefined, 10);
const convert20MilesToKm = converterOld('km', 1.60936, undefined, 20);
const convert30MilesToKm = converterOld('km', 1.60936, undefined, 30);
console.log('convert10MilesToKm : ', convert10MilesToKm); // "16.09 km"
const playWithArray = () => {
let privateArray = [];
const addItem = (item) => {
privateArray.push(item);
}
const removeItem = (index) => {
privateArray.splice(index, 1);
}
const counter = () => {
let privateCounter = 0;
const changeBy = (val) => {
privateCounter += val;
}
return ({
increment: () => {
changeBy(1);
const outerFunc = () => {
// the outer scope
let outerVar = 'I am from outside!';
const innerFunc = () => {
// the inner scope
console.log(outerVar);
}
return innerFunc;
@helabenkhalfallah
helabenkhalfallah / js-nested-scope.js
Last active May 15, 2021 20:24
JS nested scopes
const globalVar = 'global';
const outerFunc = () => {
const outerVar = 'outer';
const innerFunc = () => {
const innerVar = 'inner'
console.log(innerVar, outerVar, globalVar); // "inner", "outer", "global"
}
const outerFunc = () => {
// function scope
const message = 'Hi, Hela !';
{
// block scope
const message = 'Hello !';
console.log('inside message : ', message); // "inside message : ", "Hello !"
}
const run = () => {
// "run" function scope
const two = 2;
let count = 0;
const run2 = () => { }
console.log(two); // 2
console.log(count); // 0
console.log(run2); // function
}