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
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
}
const outerFunc = () => {
// function scope
const message = 'Hi, Hela !';
{
// block scope
const message = 'Hello !';
console.log('inside message : ', message); // "inside message : ", "Hello !"
}
@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 = () => {
// the outer scope
let outerVar = 'I am from outside!';
const innerFunc = () => {
// the inner scope
console.log(outerVar);
}
return innerFunc;
const counter = () => {
let privateCounter = 0;
const changeBy = (val) => {
privateCounter += val;
}
return ({
increment: () => {
changeBy(1);
const playWithArray = () => {
let privateArray = [];
const addItem = (item) => {
privateArray.push(item);
}
const removeItem = (index) => {
privateArray.splice(index, 1);
}
@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"
@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 / curried-function-2-converter.js
Last active May 15, 2021 23:11
JS Curried Function (Converter 2)
const converterOld2 = (toUnit) => (factor) => (offset) => (input) => {
const converterOffset = offset || 0;
return [((converterOffset + input) * factor).toFixed(2), toUnit].join(' ');
};
const kmConverterWithUnit = converterOld2('km'); // function with a single params
const kmConverterWithFactor = kmConverterWithUnit(1.60936); // function with a single params
const kmConverter = kmConverterWithFactor(undefined); // function with a single params
console.log('kmConverter : ', kmConverter);
console.log('kmConverter(10) : ', kmConverter(10)); // "16.09 km"
const universalMatcher = pattern => value => (
value ?
(value.match(pattern)&& value.match(pattern).length >= 1 ? true : false)
:false
);
const floatPattern = /^[0-9]*[,.][0-9]+$/;
const intPattern = /^[0-9]*$/;
const alphaNumeric = /^[A-Za-z0-9]$/;