[CodeWars] 8kyu#8 - Shifty Closures
■ Description:
Functional closures can get overly attached. Set them straight!
Why doesn't greet_abe() actually greet Abe?
■ Qustion
1 2 3 4 5 6 7 8 | var name = 'Abe'; var greet_abe = function() { return "Hello, " + name + '!'; }; name = 'Ben'; var greet_ben = function() { return "Hello, " + name + '!'; }; | cs |
■ My Solution
1 2 3 4 5 6 7 8 9 | var greet_abe = function() { var name = 'Abe'; return "Hello, " + name + '!'; }; var greet_ben = function() { var name = 'Ben'; return "Hello, " + name + '!'; }; | cs |
아.. 클로저,
■ Best Solution
1 2 3 4 5 6 7 8 9 10 11 | var name; function greet_abe() { name = 'Abe'; return "Hello, " + name + '!'; }; function greet_ben() { name = 'Ben'; return "Hello, " + name + '!'; }; | cs |
'Happly Coding > CodeWars' 카테고리의 다른 글
[CodeWars] 8kyu#9 - Basic Training: Add item to an Array (1) | 2016.06.06 |
---|---|
[CodeWars] 8kyu#7 - Short Long Short (0) | 2016.05.30 |
[CodeWars] 8kyu#6 - Multiply (0) | 2016.05.29 |
[CodeWars] 8kyu#5 - Broken Greetings (0) | 2016.05.29 |
[CodeWars] 8kyu#4 - Square(n) Sum (0) | 2016.05.28 |