0

I'm new to JS function and objects I'm trying to iterate an array...this is my sample code which i was trying to get the multiples of coins with an input value

var flag=0;var chipval=0;var num=0;
var main = function(coins){
        var coinsarr = [1,5,10,25,50,100,500,501];var length=coinsarr.length
        var remval = coins;
        var coinskey = "";
        while(flag==0){
            for(i=0;i<length;i++){
                if(coinsarr[i]>remval){
                    chipval = coinsarr[i-1];
                    num = parseInt(remval/chipval);
                    if(remval%chipval==0){
                        flag = 1;
                        break;
                    }else{
                        remval=remval%chipval;
                        $flag = 0;
                        break;
                    }
                }
            }
            coinskey = coinskey+","+chipval+":"+num;
        }
        coinskey = coinskey.replace(/(^,)|(,$)/g, "")
        alert(coinskey);
    }

when i run main(120), this will return 100:2,10:2(which is nothing by 100coins of 2 and 10coins of 2)

When i run main(720), i'm getting memory leaks...which says "Script on this page used too much memory"; Can anyone tell me, where the issue is or else is this the right way to proceed ??

Thanks for your time and patience...

4
  • Careful, i is leaking to global scope. Commented Jan 28, 2013 at 7:35
  • 2
    Is the $ in $flag = 0; supposed to be there? Commented Jan 28, 2013 at 7:36
  • take a look at this code chipval = coinsarr[i-1]; for i=0 Commented Jan 28, 2013 at 7:40
  • i wrote that code in PHP first and then working in JS(it was a typo error Cerbrus) i removed that $...there is some issue with my logic, i understood that Commented Jan 28, 2013 at 7:47

2 Answers 2

1

A slightly cleaner option to get a amount of coins to make up a value, would be to build a object containing the values. Something like this will do the trick:

function coins(value){
    var coinsarr = [1,5,10,25,50,100,500,501];
    var out = {};
    while(value > 0){
        var i = 0;
        while(coinsarr[i+1] <= value){
            i++; // Find the coin to add.
        }
        var coin = coinsarr[i];
        value -= coin;
        if(!out[coin]){
            out[coin] = 0;
        }
        out[coin]++;
    }
    return out;
}

coins(499);
// {        Returns:
//     1: 4
//     10: 2
//     25: 1
//     50: 1
//     100: 4
// }
coins(21359);
// {        Returns:
//     1: 2
//     5: 1
//     10: 1
//     100: 3
//     501: 42
// }

The advantage of this is that you can retrieve the values easier than having to parse a string:

var coinsObj = coins(499);
for(var key in coinsObj){
    console.log(coinsObj[key] + ' times coin "' + key + '"' );
}
// 4 times coin "1"
// 2 times coin "10"
// 1 times coin "25"
// 1 times coin "50"
// 4 times coin "100"

Or just build a string like this:

var coinsObj = coins(499);
var outString = ''
for(var key in coinsObj){
    outString += ',' + key + ':' + coinsObj[key];
}
outString = outString.substr(1); // Remove the leading comma.
Sign up to request clarification or add additional context in comments.

1 Comment

Cerbrus, thanks for the solution...i've solved it. your logic is good when compared to my logic...+100 cheers
1

If the value you pass into the function is greater than the highest value in the array you have an endless loop because the flag never gets changed.

Specifically, the condition in this if statement:

if(coinsarr[i]>remval){

...will never be true for 720 (or any other number more than 501).

Also, should $flag = 0 not be flag = 0? As it stands you create a global variable $flag that never gets used.

3 Comments

ohh no, you are right...i think there is something issue with my idiotic logic, thanks for opening my eyes nnnnnn
You're welcome. Sorry I didn't suggest a specific solution, but I'm a bit confused about what you're trying to achieve - I hope this gets you back on track though.
the array has a different types of coins(coinvalue), i'm trying to get the coinvalues and its multiple....i mean 120(100x2,10x2) or 538(500x1,25x1,10x1,1x3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.