在新函数中将数字与提示符相除

我有一个程序,根据您在提示中输入的数字计算您必须支付的钞票(2,5,10,20,...)。


我想进一步进入下一步 - 我想从第一步插入的提示中获取该数字,并将其除以数字用户类型到新提示中,以计算一个项目的平均成本。


我该怎么做?


这是我为第一部分编写的代码。


var stevilo = prompt("Vnesi znesek:");


var bankovec = [500, 200, 100, 50, 20, 10, 5, 2, 1];

var stevilo_bankovcev = 0;

var izpisi = "";

for (i = 0; i < bankovec.length; i++) {

  var y = stevilo / bankovec[i];

  if (y >= 1) {

    var razlika = Math.floor(y) * bankovec[i];

    stevilo = stevilo - razlika;

    stevilo_bankovcev = Math.floor(y) + stevilo_bankovcev;

    izpisi = izpisi + Math.floor(y) + "x" + bankovec[i] + ",";

    console.log(izpisi);

  }

}

window.onload = function() {

  document.getElementById("gremo").innerHTML = "Za plačilo je potrebno " + izpisi;

}

<span id="gremo"></span>


婷婷同学_
浏览 100回答 3
3回答

浮云间

我不确定我是否正确地理解了这个问题,但是如果您唯一想添加到程序中的是用户输入的另一个变量,然后对该变量进行划分,那么这将解决问题(尽管我不完全确定这将是什么用法):var stevilo = prompt("Vnesi znesek:");var division = prompt("Dividing by this number:");var bankovec = [500, 200, 100, 50, 20, 10, 5, 2, 1];var stevilo_bankovcev = 0;var total = stevilo/division;var izpisi = "";for (i = 0; i < bankovec.length; i++) {&nbsp; var y = stevilo / bankovec[i];&nbsp; if (y >= 1) {&nbsp; &nbsp; var razlika = Math.floor(y) * bankovec[i];&nbsp; &nbsp; stevilo = stevilo - razlika;&nbsp; &nbsp; stevilo_bankovcev = Math.floor(y) + stevilo_bankovcev;&nbsp; &nbsp; izpisi = izpisi + Math.floor(y) + "x" + bankovec[i] + ",";&nbsp; &nbsp; console.log(izpisi);&nbsp; }}window.onload = function() {&nbsp; document.getElementById("gremo").innerHTML = "Za plačilo je potrebno " + izpisi + " whereas the division's result is: " + total;}<span id="gremo"></span>基本上只需调用另一个提示,将其保存到另一个变量中,然后与它除以。

繁星点点滴滴

你需要保持一些当地的状态,试试这个:&nbsp; <span id="gremo"></span>&nbsp; <span id="avg"></span>&nbsp; <button id="add">add item</button>var pastItemPrices = [];function calcBills() {&nbsp; &nbsp; var stevilo = prompt("Vnesi znesek:");&nbsp; &nbsp; pastItemPrices.push(parseInt(stevilo));&nbsp; &nbsp; var bankovec = [500, 200, 100, 50, 20, 10, 5, 2, 1];&nbsp; &nbsp; var stevilo_bankovcev = 0;&nbsp; &nbsp; var izpisi = "";&nbsp; &nbsp; for (i = 0; i < bankovec.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var y = stevilo / bankovec[i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (y >= 1) {&nbsp; &nbsp; &nbsp; &nbsp; var razlika = Math.floor(y) * bankovec[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stevilo = stevilo - razlika;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stevilo_bankovcev = Math.floor(y) + stevilo_bankovcev;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; izpisi = izpisi + Math.floor(y) + "x" + bankovec[i] + ",";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; document.getElementById("gremo").innerHTML = "Za plačilo je potrebno " + izpisi;&nbsp; &nbsp; document.getElementById("avg").innerHTML = "Average price: " + pastItemPrices.reduce((total, price) => total + price) / pastItemPrices.length}window.onload = function() {&nbsp; &nbsp; document.getElementById("add").addEventListener("click", calcBills);&nbsp; &nbsp; calcBills();}

弑天下

像这样的东西?function znesek() {&nbsp; var stevilo = prompt("Vnesi znesek:");&nbsp; const stevilo1 = stevilo; // take a copy&nbsp; var bankovec = [500, 200, 100, 50, 20, 10, 5, 2, 1];&nbsp; var stevilo_bankovcev = 0;&nbsp; var izpisi = "";&nbsp; for (i = 0; i < bankovec.length; i++) {&nbsp; &nbsp; var y = stevilo / bankovec[i];&nbsp; &nbsp; if (y >= 1) {&nbsp; &nbsp; &nbsp; var razlika = Math.floor(y) * bankovec[i];&nbsp; &nbsp; &nbsp; stevilo = stevilo - razlika;&nbsp; &nbsp; &nbsp; stevilo_bankovcev = Math.floor(y) + stevilo_bankovcev;&nbsp; &nbsp; &nbsp; izpisi = izpisi + Math.floor(y) + "x" + bankovec[i] + ",";//&nbsp; &nbsp; &nbsp; console.log(izpisi);&nbsp; &nbsp; }&nbsp; }&nbsp; document.getElementById("gremo").innerHTML = "Za plačilo je potrebno " + izpisi;&nbsp; setTimeout(function() { drugZnesek(stevilo1) } ,10); // allow DOM update}function drugZnesek(stevilo1) {&nbsp; var drugStevilo =&nbsp; prompt("Vnesi drug znesek")&nbsp; if (stevilo1 && isNaN(stevilo1) || isNaN(drugStevilo)) {&nbsp; &nbsp; document.getElementById("gremo").innerHTML = "Žal ne številk";&nbsp; &nbsp; return;&nbsp; }&nbsp; else {&nbsp; &nbsp; console.log(stevilo1,drugStevilo,stevilo1/drugStevilo)&nbsp; &nbsp; document.getElementById("gremo").innerHTML += "<br/>"+(stevilo1/drugStevilo).toFixed(2)&nbsp; }}&nbsp;&nbsp;window.addEventListener("load", znesek)<span id="gremo"></span>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript