Skip to content

Commit

Permalink
hw22 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
eduard.poteshnov committed Dec 9, 2021
1 parent fc8b54d commit e97f652
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions hwLesson13/js/hw22Closures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//

function getCounter(number){
function getCounter(number) {
return {
number: typeof number === 'number' && !Number.isNaN(number) ? number : 0,
value: function () {return this.number},
Expand All @@ -19,21 +19,14 @@ function getCounter(number){
}
}

const counter = getCounter(5);
const counter2 = getCounter(33);
const counter3 = getCounter("Byba");
const counter4 = getCounter(NaN);

console.log(counter === counter2); //should be false
console.log(counter.value()); // should be 5
console.log(counter2.value()); // should be 33
console.log(counter3.value()); // should be 0
console.log(counter4.value()); // should be 0

counter.decrease();
counter.decrease();
counter3.increase();
counter3.increase();

console.log(counter.value()); //should be 3
console.log(counter3.value()); //should be 2
function getCounter2(number) {
function Counter(number) {
let num = (typeof number === 'number' && !Number.isNaN(number) ? number : 0);
return {
value : (() => {return num})(),
increase : (() => {num++})(),
decrease : (() => {num--})()
}
}
return new Counter(number);
}

0 comments on commit e97f652

Please sign in to comment.