javascript - Closure - change for coins -


i want understand closure concept, practicing exercise. totally lost , confused. task find minimum of coins needs used fulfil total.as result £7.60 have array res = [2, 2, 2, 1, 0.5, 0.1]

here have far:

function sortcoins (money, coins){   res = [];  if(money % coins[0] !== 0){   return  function(){     do{       money = money - coins[0];       res.push(coins[0]);     }     while(coins[0]<= money);     coins.shift();   };   // coins.shift(); } else {   do{       money = money - coins[0];       res.push(coins[0]);     }     while(money !== 0 ); }  return res;   }  sortcoins (17, [5, 2, 1, 0.5, 0.25, 0.1  ])(); 

i appreciate help, explanation , suggestion read , practice understand closure better. saw solutions similar coins problem, not want use it, need understand doing wrong in code.

you return function inside sortcoins, kinda weird return res inside sortcoins.

also, have not defined res variable, don't have added var before it, accessing not defined global variable.

and final one, tip, keep indentation clean. have mess of spaces makes code blocks difficult understand @ first sight.

in short, problems returns. let me fix that:

the function:

function sortcoins(money, coins){     var res = [];     // use index instead of modifying array. keep parameters immutable when possible.     var current = 0;     {         money = money - coins[current];         res.push(coins[current]);         while (money < coins[current]) {             current++;         }     } while(money > 0);     return res; }  // console.log it, see in f12 console tab console.log(sortcoins(17, [5, 2, 1, 0.5, 0.25, 0.1])); 

closuring it:

we going create set of coins, this:

function sortcoins(coins) {     // notice function returns function     return function(money) {         var res = [];         // use index instead of modifying array. keep parameters immutable when possible.         var current = 0;         {             money = money - coins[current];             res.push(coins[current]);             while (money < coins[current]) {                 current++;             }         } while(money > 0);         return res;     }; }  var mycoins = sortcoins([5, 2, 1, 0.5, 0.25, 0.1]); mycoins(17);  var mycoins_small = sortcoins([0.5, 0.25, 0.1]); mycoins_small(17);  mycoins(12); mycoins_small(12); 

take @ contents of sortcoins function. correct indentation easy see returns function. not looking @ contents of returned function can see return has:

function sortcoins(coins) {     // notice function returns function     return function(money) {         [...]     }; } 

so, if call sortcoins([5, 2, 1, 0.5, 0.25, 0.1]) return function coins argument set [5, 2, 1, 0.5, 0.25, 0.1].

var mycoins = sortcoins([5, 2, 1, 0.5, 0.25, 0.1]); 

now have variable mycoins, function, coins argument set [5, 2, 1, 0.5, 0.25, 0.1]. in other words, having piece of code:

var coins = [5, 2, 1, 0.5, 0.25, 0.1]; function mycoins(money) {     var res = [];     // use index instead of modifying array. keep parameters immutable when possible.     var current = 0;     {         money = money - coins[current];         res.push(coins[current]);         while (money < coins[current]) {             current++;         }     } while(money > 0);     return res; }; 

what happens if call mycoins(17); in last piece of code? if closely, accesses coins variable, if change coins variable [0.5, 0.25, 0.1] receive different output.

how change it? going first sortcoins(coins) function, easy calling coins attribute:

var mycoins_small = sortcoins([0.5, 0.25, 0.1]); 

now mycoins_small has coins attribute set different array , returns function. have 2 functions, mycoins , mycoins_small, each 1 running inside own context have 2 different coins attributes set.

in short. closures in js limited functions. when tell code variable, inside current context (which own function). if don't finds variable, go 1 level (that say, on parent function) , there, , if don't finds there, go level, , on until reaches called "global scope" (in other words, main level first lines of code run).

here can see easier:

var mainlevel = 0; // variable declared in global scope, exists in functions  function level1() {     var level1variable = 1; // variable declared inside level1 function, exists in level1 function , descendants      function level2() {         var level2variable = 2; // variable declared inside level2 function, exists in level2 function , descendants          // level2 has access own variables , 1 in parents         console.log(level2variable, level1variable, mainlevel);     }      // if level1 access level2variable, return error cannot access     console.log(level2variable);       // can access own variables , parent ones     console.log(level1variable, mainlevel);  } 

with , knowing js keeps context of returned functions, can awesome things currying (which thing made first sortcoins(coins) function).

if bit lost, note that

function pepe(arg) {     return arg * 2; } 

is same as

var pepe = function(arg) {     return arg * 2; }; 

both can called pepe(2) returning same output. have minor differences not going enter details not mess head more.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -