Bittercold
Bittercold

Reputation: 23

Count how many times a while loop runs. javascript

I have a list of numbers var coins = [16, 8, 4, 2, 1]; and I am supposed to ask the user to enter a number then find out what combination of numbers are needed to equal the number the user entered, then display that information to the user(e.g. how many times did you use any given number.)

here's the code.

//list of numbers, and the variable that will hold the math for the program
var coins = [16, 8, 4, 2, 1];
var value = 0;

//ask the user for a number
var number = Number(prompt('please enter a number between 20 and 100.'));

//counting loops and list locations
var i = 0;
var count = 0;

//filter out numbers under 20 and over 100
if (number < 20 || number > 100){
    alert("Invalid number");
} 


while (value != number ) {
    //run the loop while a number from the list + the value is less than or equal to the number the user entered 
    while(coins[i] + value <= number){
        value += coins[i];
        console.log(coins[i]);

        //count how many times the loop runs. currently its only doing the first position of the list which seems wrong.
        if (coins[i] == coins[0]){
            count++;
        }
    }
    i++;
}

console.log(value);
console.log(number);
console.log(i);
console.log(count);

I want to count the number of times each number is used, but I cant really count the number of times the loop runs because they will sometimes be different numbers in the loop thus making the count++ wrong. in the chrome console log console.log(coins[i]); shows a little number next to the coins[i] number, what exactly is that number and how would I go about getting it because it seems to be exactly what i need.

right not i'm just using

if (coins[i] == coins[0]){
        count++;
    }

because i don't think there is a number that will result in any repeats besides the first number 16 but this feels like a cheap work around.

Upvotes: 1

Views: 7263

Answers (3)

Sgene9
Sgene9

Reputation: 186

I think you are asking how to get how many times each number was used.

Like if the input is 20, 16 is used once and 4 is used once.

Apparently, you need 5 counters for each numbers in {16, 8, 4, 2, 1}.

You will need to declare

var countCoins = [0, 0, 0, 0, 0];

and instead of the

if (coins[i] == coins[0]){
        count++;
    }

put

countCoins[i]++;

At the end, countCoins[i] will have the number of times that coins[i] was used

You can decided to add them all together or keep them separate.

Upvotes: 1

Ezenhis
Ezenhis

Reputation: 1007

Use an object like this:

var counts = {"16":0, "8":0, "4":0, "2":0, "1":0}

Instead of the single int count and then use as:

counts[i]++;

Upvotes: 0

aaron
aaron

Reputation: 43088

You can create a dictionary counter and initialize it like this:

var num = {};
coins.forEach(function (coin) {
    num[coin] = 0;
});

Usage:

while (value != number) {
    while(coins[i] + value <= number){
        value += coins[i];
        num[coins[i]]++;
    }
}

console.log(num);

Upvotes: 0

Related Questions