简体   繁体   English

JS中的简单计算器

[英]Simple calculator in JS

I create a simple web app in pure JS and generally I have few issues which I don't know how to solve. 我用纯JS创建了一个简单的Web应用程序,通常我遇到了一些我不知道如何解决的问题。 First of all, you can see my demo tool here: http://codepen.io/testerius/pen/EaOPEY 首先,您可以在这里看到我的演示工具: http : //codepen.io/testerius/pen/EaOPEY

function calculator() {

/* 
    SPEARMAN
    this code below is for Spearman unit
*/

// resource requirements
var spearmanWood = 50;
var spearmanClay = 30;
var spearmanIron = 20;

var spearman = document.getElementById("spearman").value;

// calculate
var spearmanAmount = spearman;
var spearmanWood = spearmanWood * parseInt(spearman);
var spearmanClay = spearmanClay * parseInt(spearman);
var spearmanIron = spearmanIron * parseInt(spearman);
var spearmanProvisions = spearman;
var spearmanTime = spearman * 136; // seconds

// calculate time
var totalSec = spearmanTime;
var hours   = Math.floor(totalSec / 3600);
var minutes = Math.floor((totalSec - (hours * 3600)) / 60);
var seconds = totalSec - (hours * 3600) - (minutes * 60);
var spearmanTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes<10 ? "0" + minutes : minutes) + ":" + (seconds<10 ? "0" + seconds : seconds);

// print to table
document.getElementById("spearmanAmount").innerHTML = spearmanAmount;
document.getElementById("spearmanWood").innerHTML = spearmanWood;
document.getElementById("spearmanClay").innerHTML = spearmanClay;
document.getElementById("spearmanIron").innerHTML = spearmanIron;
document.getElementById("spearmanProvisions").innerHTML = spearmanProvisions;
document.getElementById("spearmanTime").innerHTML = spearmanTime;


/* 
    SWORDSMAN
    this code below is for Swordsman unit
*/

// resource requirements
var swordsmanWood = 30;
var swordsmanClay = 30;
var swordsmanIron = 70;

var swordsman = document.getElementById("swordsman").value;

// calculate
var swordsmanAmount = swordsman;
var swordsmanWood = swordsmanWood * parseInt(swordsman);
var swordsmanClay = swordsmanClay * parseInt(swordsman);
var swordsmanIron = swordsmanIron * parseInt(swordsman);
var swordsmanProvisions = swordsman;
var swordsmanTime = swordsman * 194; // seconds

// calculate time
var totalSec = swordsmanTime;
var hours   = Math.floor(totalSec / 3600);
var minutes = Math.floor((totalSec - (hours * 3600)) / 60);
var seconds = totalSec - (hours * 3600) - (minutes * 60);
var swordsmanTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes<10 ? "0" + minutes : minutes) + ":" + (seconds<10 ? "0" + seconds : seconds);

// print to table
document.getElementById("swordsmanAmount").innerHTML = swordsmanAmount;
document.getElementById("swordsmanWood").innerHTML = swordsmanWood;
document.getElementById("swordsmanClay").innerHTML = swordsmanClay;
document.getElementById("swordsmanIron").innerHTML = swordsmanIron;
document.getElementById("swordsmanProvisions").innerHTML = swordsmanProvisions;
document.getElementById("swordsmanTime").innerHTML = swordsmanTime;


/* 
    SUM OF ALL UNITS
    this code below is for calculate all units
*/

// all
var allAmount = parseInt(spearmanAmount) + parseInt(swordsmanAmount);
var allWood = parseInt(spearmanWood) + parseInt(swordsmanWood);
var allClay = parseInt(spearmanClay) + parseInt(swordsmanClay);
var allIron = parseInt(spearmanIron) + parseInt(swordsmanIron);
var allProvisions = parseInt(spearmanProvisions) + parseInt(swordsmanProvisions);
var allTime = spearmanTime + swordsmanTime;


// all print
document.getElementById("allAmount").innerHTML = allAmount;
document.getElementById("allWood").innerHTML = allWood;
document.getElementById("allClay").innerHTML = allClay;
document.getElementById("allIron").innerHTML = allIron;
document.getElementById("allProvisions").innerHTML = allProvisions;
document.getElementById("allTime").innerHTML = allTime;

} }

How it should work: user type how many units he would create, then JS code makes for him all calculation requirements. 工作方式:用户输入要创建的单位数量,然后JS代码为他提供所有计算要求。

As you can see it works but there are few bugs which I'd like to fix but my lack of knowledge doesn't help me. 如您所见,它可以工作,但是我想修复的bug很少,但是我的知识不足无济于事。 :P :P

Problem #1 - how can I hide the requirements table and show it only when user click button Calculate? 问题1-如何隐藏需求表并仅在用户单击“计算”按钮时显示? CSS? CSS?

Problem #2 - button Reset clears inputs but doesn't clear results from requirements table, how can I make it work? 问题2-“重置”按钮清除输入,但未清除需求表中的结果,如何使它起作用?

Problem #3 - times aren't added as I want, well it's probably string bug but I don't have any idea how to solve it. 问题#3-我没有添加时间,这很可能是字符串错误,但是我不知道如何解决。

Problem #4 - as you can see I repeat some of code, for spearman and swordsman code is very similar. 问题4-如您所见,我重复了一些代码,因为长矛手和剑客的代码非常相似。 Do you have any idea how I can make it less repeat? 您是否知道如何减少重复?

Ok, so that's all what I wanted to ask you guys. 好的,这就是我要问你们的全部。 Hope someone could help me. 希望有人可以帮助我。 Don't get wrong but I quite beginner programmer so my code can be... you know not good. 没错,但是我是个初学者,所以我的代码可以是……你知道不好。 :P :P

For problem 1, I believe this is a solution . 对于问题1,我相信这是一个解决方案 I'm new to Javascript so pretty limited in how I can help. 我是Java的新手,所以我只能提供有限的帮助。

Here are the lines of code I changed: 这是我更改的代码行:

In HTML: 在HTML中:

<div class="row" id="requirements">

In CSS: 在CSS中:

#requirements {
    display:none;
}

In JS: 在JS中:

document.getElementById("requirements").style.display="block";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM