-1

I made a little program to find minimum number of notes(currency) for the required amount. for example, let's say I enter an amount 1121 and I have an array of these values below: notes = [1, 2, 5, 10, 50, 100, 200, 500] so my final result will be:

500 * 2(notes) = 1000

100 * 1 = 100

20 * 1 = 20

1 * 1 = 1

then the total would be 1121. any help with understanding would be much appreciated. I know it only takes one for loop but I'm confused in some parts.

here's what I did: https://stackblitz.com/edit/angular-rufwzk?file=src%2Fapp%2Fapp.component.ts

  • What problem are you having with your code? Im not sure what the question is... – user184994 Sep 15 '18 at 18:52
  • which parts are you confused with? – Chris Li Sep 15 '18 at 18:53
  • i want to store the amount of each value and its quotient to check if it's equal to entered amount or not **entered amount:** 1121 array = [1, 2, 5, 10, 50, 100, 200, 500]; starting from reverse 1) 1121 / 500 = 500 * 2 = 1000, remainder 121 and quotient is 2 2) 121 / 200 not possible 3) 121 / 100 = 100 * 1 = 100, remainder 21 and quotient is 1 4) 21 / 50 not possible 5) 21 / 10 = 10 * 2 = 20, remainder is 1 and quotient 2 6) 1 / 5 not possible my final result is which currency note: 500, 100, 10 number of each currency: 2, 1, 2 (quotients) – confusedGuy Sep 15 '18 at 19:13
  • @user184994 my question is i want to console each currency note and its repeated numbers. consider an array as currency array. e.g entered amount 1000 then i would require 500rs note 2 times. – confusedGuy Sep 15 '18 at 19:18
  • @confusedGuy please enter all the explanations in you post. – JSmith Sep 15 '18 at 20:48

3 Answers3

0

Here is the corrected code of what you want. I hope i get it right.

  requiredNotes(amount) {
    let noteArry = this.notes,
      quotient,
      remainder,
      temp,
      noteCount = 0,
      eachNote,
      remainingAmount = 0;
    for (let i = noteArry.length - 1; i >= 0; i--) {
      if (amount >= noteArry[i]) {
        quotient = Math.floor(amount / noteArry[i]);
        remainder = amount % noteArry[i];
        amount = amount - (noteArry[i] * quotient);
        if (amount == 0) {
          console.log("note:-", noteArry[i], ",number of note", quotient);
          break;
        } else if (amount != 0) {
          console.log("note:-", noteArry[i], ",number of note", quotient);
        }
      } else {
                continue;
      }
    }
  }
Exterminator
  • 1,221
  • 7
  • 14
0
for (let i = noteArry.length - 1; i >= 0; i--) {
  if (amount >= noteArry[i]) {
    quotient = Math.floor(amount / noteArry[i]);
    remainder = amount % noteArry[i];
    remainingAmount = noteArry[i] * quotient;
    amount=amount-remainingAmount;
    console.log('number of notes =', noteArry[i], 'x', quotient,' notes');
  }
}

This does the trick simply logs out the number of notes for the mentioned amount.

Andriy
  • 14,781
  • 4
  • 46
  • 50
Vaibhav
  • 1,481
  • 13
  • 17
0

I simplified a little your code (using JS Map):

...
notesMap = new Map();
...

requiredNotes(amount) {
  for (let i = this.notes.length - 1; i >= 0 && amount; i--) {
    const qty = Math.floor(amount / this.notes[i]);
    qty && this.notesMap.set(this.notes[i], qty);
    amount = amount % this.notes[i];
  }

  const entries = Array.from(this.notesMap.entries());
  this.requireNotes = entries.map(([curr, qty]) => `${curr} * ${qty} = ${curr * qty}`);
}

STACKBLITZ

Andriy
  • 14,781
  • 4
  • 46
  • 50