FreeCodeCamp---advanced Algorithm Scripting解法

Exact Change

设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数.

cid 是一个二维数组,存着当前可用的找零.

当收银机中的钱不够找零时返回字符串 "Insufficient Funds". 如果正好则返回字符串 "Closed".

否则, 返回应找回的零钱列表,且由大到小存在二维数组中.

 1 function checkCashRegister(price, cash, cid) {
 2   var arr=[];  //应找回的零钱列表
 3   var cashPrice=[];  //一种钱代表的金额
 4   var cashName=[];  //钱的名字
 5   var cashItemSum=[];  //一种钱的总数
 6   var change=Math.round((cash-price)*100)/100;   //要找回的零钱
 7   var sumCash=0;
 8   cid.forEach(function(item,index,array){
 9     if(item[1]>0){
10       cashName.push(item[0]);
11       cashItemSum.push(item[1]);
12       sumCash += item[1];
13     }
14   });
15
16   cashPrice=cashName.map(function(item,index,array){
17     switch (item){
18       case "PENNY":
19         return 0.01;
20       case "NICKEL":
21         return 0.05;
22       case "DIME":
23         return 0.1;
24       case "QUARTER":
25         return 0.25;
26       case "ONE":
27         return 1;
28       case "FIVE":
29         return 5;
30       case "TEN":
31         return 10;
32       case "TWENTY":
33         return 20;
34       case "ONE HUNDRED":
35         return 100;
36     }
37   });
38
39
40   sumCash=Math.round(sumCash*100)/100; //对sumCash保留两位小数
41
42   switch (true){
43     case change>sumCash:
44       return "Insufficient Funds";
45     case change === sumCash:
46       return "Closed";
47     default:
48       approachChange(change);
49       if(arr.length===0){
50         return "Insufficient Funds";
51       }else{
52         return arr;
53       }
54   }
55
56
57
58   function approachChange(change){
59     var index=0;
60     if(change>cashPrice[cashPrice.length-1]){
61       index=cashPrice.length-1;
62     }
63
64     for(var i=0; i<cashPrice.length-1; i++){
65       if(cashPrice[i]<=change && change<cashPrice[i+1] && cashItemSum[i]!==0){
66          index=i;
67       }
68     }
69
70     var itemNeedSum=Math.floor(change/cashPrice[index]) * cashPrice[index];
71     var needMin=Math.min(itemNeedSum,cashItemSum[index]);
72     var x=Math.round( (change-needMin)*100 )/100;
73
74     if(x>0 && index===0){
75       return;
76     }
77
78     arr.push([cashName[index], needMin]);
79
80
81     if(x===0){
82       return;
83     }else{
84       cashPrice=cashPrice.slice(0,index);
85       approachChange(x);
86     }
87   }
88
89 }
90
91
92 checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

时间: 2024-10-11 11:38:56

FreeCodeCamp---advanced Algorithm Scripting解法的相关文章

FCC Advanced Algorithm Scripting

1.Validate US Telephone Numbers 如果传入字符串是一个有效的美国电话号码,则返回 true. 用户可以在表单中填入一个任意有效美国电话号码. 下面是一些有效号码的例子(还有下面测试时用到的一些变体写法): 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555 1 555-555-5555 1 (555) 555-55551(555)555-5555 在本节中你

advanced dom scripting dynamic web design techniques Part One DOM SCRIPTING IN DETAIL CHAPTER 1 DO IT RIGHT WITH BEST PRACTICES

You’re excited; your client is excited. All is well. You’ve just launched the client’s latest website, and it’s fantastic. You’ve put in hours of sweat and tears, tweaking every little detail of the design—expanding menus, interactive Ajax, all the l

高级算法设计讲义 Lecture Notes for Advanced Algorithm Design

(Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approximation Algorithms, Cambridge University Press, 2011. (2) Vijay Vazirani. Apporoximation Algorithms. Springer, 2001. (3) Rajeev Motwani, Prabhakar Ragh

advanced dom scripting dynamic web design techniques Chapter 2 CREATING YOUR OWN REUSABLE OBJECTS

JavaScript is all about objects. Objects are the foundation of everything, so if you’re unfamiliar with objects, you’re going to learn quickly. The goal of this book is not to be a JavaScript or DOM code reference, but in order to make sure you under

一个平庸程序员自白:我不牛逼但那又怎样?

转自:http://www.managershare.com/post/189966 不是谁都能飚高音,但他们也有自己的声音. Jacob Kaplan-Moss是著名 Python 框架 Django 的 Co-Creater 和核心开发者,在 Heroku 担任安全部门的 Director, 他常年参加 Python 社区的年度聚会 PyCon ,并经常上台演讲.在这次 PyCon 2015 上,他声称自己顶多只是一个平庸的程序员,幻灯片上也写着: “Hi, I’m Jacob, and I

视频教程

英雄联盟教程     http://pan.baidu.com/s/1i3rkMS9 密码:bv6r https://pan.baidu.com/share/link?shareid=2589856556&uk=371904234#list/path=%2F%E6%88%91%E7%9A%84%E8%B5%84%E6%BA%90%2F%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B%E9%9B%86%E5%90%88%2F%E7%BD%91%E7%BB%9C%E6%95%

Linux/Unix 新手和专家教程

你正在找一些高质量的Linux 和 UNIX 的教程吗?如果是,这篇文章会告诉你到哪去找到这些教程.这里我们将给出超过30个相当的不错的 Linux 和 UNIX 在线的教程. 需要大家注意的是,他们都是英文的,也许有一些也经被翻译到了中文社区,你可以搜索一下.但不管怎么样,我的建议是应该尽可能的去阅读英文. Linux 和UNIX 的新手培训教程 免费的新手Linux教程 Introduction to Linux – 这是来自IBM的教程,用于给那些想学习Linux的人. Linux Des

linux学习书籍推荐linux学习书籍推荐

引用地址:http://www.cnblogs.com/notepi/archive/2013/06/15/3137103.html Linux 学习书目推荐 Linux基础 1.<Linux与Unix Shell 编程指南> C语言基础 1.<C Primer Plus,5th Edition>[美]Stephen Prata著 2.<The  C Programming Language, 2nd Edition>[美]Brian W. Kernighan Davi

高级语言与低级语言

[高级语言与低级语言] 本文记录Mono如何让你工作更高效,以及如何让软件在无需把C/C++重写成C#代码的情况下拥有扩展性. In the past, software used to be written entirely in a single programming language. Developers had to strike a balance between high performance and having to pick a low-level language or