cash-machine.js

    0

    0

    This code will give the user the option of withdrawing up to 160 R$.

    
    //*** CASH MACHINE ***//
    
    //THE PROBLEM
    //Develop a solution that simulate the delivery of notes when a client does a withdraw in a cash machine.
    
    //THE BASIC REQUIREMENTS ARE THE FOLLOW:
    // Always deliver the lowest number of possible notes;
    // It's possible to get the amount requested with available notes;
    // The client balance is infinite;
    // Amount of notes is infinite;
    // Available notes R$ 100,00; R$ 50,00; R$ 20,00 e R$ 10,00
    
    (function(){
      'use strict';
    
      function iWantToGet(ammountRequired){
        var availableNotes = [100, 50, 20, 10],
            result = [];
    
        if(ammountRequired && ammountRequired > 0) {
          while(ammountRequired > 0) {
            for (var i = 0; i < availableNotes.length; i++) {
              var note = availableNotes[i];
              if(ammountRequired - note >= 0) {
                ammountRequired -= note;
                result.push(note);
                break;
              } else if(i === availableNotes.length - 1 && ammountRequired > 0) {
                  throw 'NoteUnavailableException';
              }
            }
          }
        } else if(ammountRequired <= 0) {
          throw 'InvalidArgumentException';
        }
    
        return result;
      }
    
      console.log(iWantToGet(160));
    
    })();
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.