Good QUestions 2

by rishul matta

JavaScript

A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and
$1. Your task is to write a program that will be used in a vending machine to
return change. Assume that the vending machine will always want to return the
least number of coins or notes. Devise a function getChange(M, P) where M is
how much money was inserted into the machine and P the price of the item
selected, that returns an array of integers representing the number of each
denomination to return.

Example: getChange(5, 0.99) should return [1,0,0,0,0,4]


function getChange(M, P) {
  let returnCurrency = [0,0,0,0,0,0];
  let totalMoney = parseInt(M *100);
  let moneyWit = parseInt(P * 100);

  let moneyReturn = totalMoney - moneyWit;

  if (M <= 0 || P <= 0 || moneyReturn <=0) {
    return returnCurrency;
  }

  
  let valueArr = [1,5,10,25,50,100];

  while(moneyReturn > 0) {
    for (var i = 0; i<valueArr.length; ++i ) {
      if (valueArr[i] < moneyReturn){
        continue;
      } else {
        break;
      }

    }
    if (i > 0 && valueArr[i] != moneyReturn) {
      i = i -1;
    }
    let curr = valueArr[i];
    returnCurrency[i] += 1;
    moneyReturn -= curr;
  }

  return returnCurrency;
}






A precedence rule is given as "P>E", which means that letter "P" is followed
by letter "E". Write a function, given an array of precedence rules, that
finds the word represented by the given rules.

Note: Each represented word contains a set of unique characters, i.e. the word
does not contain duplicate letters.

Examples: - findWord(["P>E","E>R","R>U"]) -> PERU -
findWord(["I>N","A>I","P>A","S>P"]) -> SPAIN

function findWord(arr) {

  let map = {};
  let str = [];
  arr.forEach((entry) => {
    let ar = entry.split(">");
    map[ar[0]] = ar[1];
  });

let ptr;
  while(Object.keys(map).length > 0) {
    let value;
    for ( ptr in map) {
      value = map[ptr];
      if (map[value] == undefined) {
        str.push(value);
        delete map[ptr];
        break;
      }
    }
 ...