- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 javascript/jquery 中创建一个函数来计算将最少的硬币数量加起来达到输入的总金额。
所以我有一个硬币对象数组:
var coins = [
{
pennies: 200,
print: '£2'
},
{
pennies: 100,
print: '£1'
},
{
pennies: 50,
print: '50p'
},
{
pennies: 20,
print: '20p'},
{
pennies: 10,
print: '10p'
},
{
pennies: 5,
print: '5p'},
{
pennies: 2,
print: '2p'
},
{
pennies: 1,
print: '1p'
}
];
var $input = $('input');
$input.keypress(function(e) {
if (e.which == 13) {
// do something
}
});
还有我的 HTML 中的输入字段。
因此,如果有人输入 123p,我希望表单返回 1 x £1、1 x 20p、1 x 2p 和 1 x 1p。
我正在努力弄清楚从哪里开始。如果有人可以提供帮助,我们将不胜感激。
谢谢
最佳答案
听起来您只需要从最高面额开始遍历 coins
数组:
const coins=[{pennies:200,print:'£2'},{pennies:100,print:'£1'},{pennies:50,print:'50p'},{pennies:20,print:'20p'},{pennies:10,print:'10p'},{pennies:5,print:'5p'},{pennies:2,print:'2p'},{pennies:1,print:'1p'}];
const getCoins = penniesToGo => coins.reduce((coinCountStr, { pennies, print }) => {
const numCoins = Math.floor(penniesToGo / pennies);
if (numCoins < 1) return coinCountStr;
penniesToGo -= numCoins * pennies;
const thisCoinStr = `${numCoins}x ${print}`;
return coinCountStr ? coinCountStr + ', ' + thisCoinStr : thisCoinStr;
}, '');
console.log(getCoins(201));
console.log(getCoins(333));
console.log(getCoins(6));
关于javascript - 给定数量要使用的最少硬币数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50730877/
我目前正在做一个项目,试图开发一种用于 P2P 能源交易的货币和模型,其中每产生一千瓦时的可再生能源就会向该生产商类型转换一枚硬币。我的问题是关于销毁智能合约中的代币。 所有与我的项目类似的当前实现都
在没有Maps帮助的情况下通过Memoization解决问题,由于读取文件的方法,我得到了TLE,根据我的说法,这不应该是这种情况。可能的原因是什么? 这是给出 AC - http://ideone.
考虑下面这段伪代码,其中d是面额值数组,k是面额数,n是要进行更改的金额。 Change(d; k; n) 1 C[0] 我真的不明白这部分,你为什么要用它,谁能给我解释一下! 最佳答案 为了回答
我正在尝试在我的网站上实现 Coin Slider (http://workshop.rs/2010/04/coin-slider-image-slider-with-unique-effects/)
我有使用硬币 slider 的画廊 var $jq = jQuery.noConflict(); $jq(window).load(function() { var imhei
我使用了从该站点提取的硬币 slider http://workshop.rs/projects/coin-slider/ .它现在自动滚动并仅在悬停时显示上一个和下一个。我需要禁用自动滚动并正常显示
我的问题是一道CodeFu练习题(2012 round 2 problem 3)。它基本上归结为将整数数组分成两个(几乎)相等的两半并返回两者之间可能的最小差异。我在下面包含了问题描述。如评论中所述,
我们的老师要求我们制作一 jar 硬币,用来计算我们有多少便士、一毛钱等,然后给出总金额。 这是他希望我们使用的模板 https://online.pcc.edu/content/enforced/7
我正在尝试使用币安币 future 的 api 下载 BTC/USD 永续 future 的历史价格数据,具体来说,我想使用 this endpoint .但是,我找不到必须为 BTC/USD 指定的
我上周刚开始学习计算机科学,我们得到了一个名为“硬币”的工作表,其中我必须找出一组硬币中有多少个 25 美分、10 美分、5 美分和 10 便士。我遇到了很多麻烦,并收到了该错误。这是我的代码 pac
我正在构建一些使用消耗性硬币的测验。我使用 NSUserDefault 来保存设备上的硬币及其工作。我还在 qiuz 中使用 CloudKit 处理数据。 不麻烦的是,如果用户切换设备如何恢复硬币?有
我是一名优秀的程序员,十分优秀!