简体   繁体   English

基于商品价格和总价的Javascript计算器

[英]Javascript calculator based on item price and total price

I need to make some kind of calculator for trading some items in the game and honestly javascript calculator would be best for me because it can calculate on the go probably. 我需要制作某种计算器来交易游戏中的某些物品,说实话,javascript计算器对我来说是最好的,因为它可以随时随地进行计算。

But i really dont know from where to start. 但是我真的不知道从哪里开始。

I need to calculate items in game how much it worth if i get gold for them. 我需要计算游戏中的物品,如果我得到它们的金牌价值多少。

For example someone is giving me 168 gold and i give him items and there are items like helmet, vest, boots, gloves etc... 例如,有人给我168金,我给他物品,还有头盔,背心,靴子,手套等物品。

So the item prices are like this 所以商品价格是这样的

Vest is 60 gold
Helmet is 45 gold

And as spare change 作为零钱

Tokens 25 gold
charms 7 gold

So if someone want to give me 168 gold he can select in vest or helmet and tokens or charms as spare change 因此,如果有人想给我168金币,他可以选择背心或头盔以及令牌或护身符作为零钱

So for 168 gold 

He gets 2 vest and 7 charms

And for example if he wants to exchange 325 gold in helmets he gets

7 helmets and 1 charm.

And so on. 等等。

Does anyone have idea how complicated this is and from where to start. 有谁知道这有多复杂以及从哪里开始。

Think of it as i am seller in market and someone comes to me telling me he have 100$ and how many chocolates he can get for that. 想象一下,我是市场上的卖方,有人来告诉我他有100美元,为此他可以买多少巧克力。

Start by learning what the javascript is (and what it isn't ). 首先学习什么是javascript什么不是 )。 (by this sentence I originally refer to the fact that the question was tagged both Java and JavaScript) (通过这句话,我最初指的是该问题同时被标记为Java和JavaScript)

I'm afraid you've expected that someone could post you whole code that you can copy and paste. 恐怕您期望有人可以将您可以复制和粘贴的整个代码发布给您。 I will disappoint you - this is a knowledge site, not a coding machine. 我会让您失望的-这是一个知识网站,而不是编码机器。

While I did not understand what exactly you want to do (and I doubt anyone did) I can give you a few hints on How to start? 虽然我不明白您到底想做什么(我怀疑有人这样做了),但我可以给您一些有关如何开始的提示 phase of thing. 事物的阶段。

I assume you have read what javascript is here: 我假设您已阅读此处的javascript:

To let the user input numbers, create HTML input elements (outside of the script): 要让用户输入数字,请创建HTML输入元素(在脚本外部):

<input type="text" id="UNIQUE_ID" value="" />
<button onclick="click()">Click when done!</button>
<script> ... put the code between script tags... </script>

I have declared that whenever the <button> is clicked ( "onclick" ) the function click() will be called. 我已经声明,只要单击<button>“ onclick” ),就会调用click()函数。 Let's create click() : 让我们创建click()

function click() {
    //We can get an "object" that represents the input field
    var field = document.getElementById("UNIQUE_ID");
    //We can get the value of said field
    var number = field.value;
    //We can empty the field now:
    field.value = "";
    //The number is now string (=text) We need a number. Multiplication forces conversion to number
    number = 1*number;
    //Check if the number is really a number
    if(isNaN(number))
        throw new Error("Input is not a number!");
    //Do some calculation - for example conversion to hex:
    var hexagonal = "0x"+(number).toString(16).toUpperCase();
    //Throw the number at the user - nasty but easy
    alert(number);

}

Depending on what you want to calculate, you can modify my function. 根据您要计算什么 ,你可以修改我的功能。 You can also have more fields and more numbers. 您还可以具有更多的字段和更多的数字。

I have noticed that for any amount of money you can buy something and charms . 我注意到,无论花多少钱,您都可以买到东西饰品 Unfortunately summer is hot and my crystal ball has overheated - so I can't let it tell me what charms are. 不幸的是夏天很热,我的水晶球过热了-所以我不能让它告诉我什么是魅力

But to find out how many you can buy: 但要找出可以购买的数量:

var gold = 666;
//D'oh an object!
var prices = {
  helmet: 150,
  armor: 430,
  ice_cream: 10,
  charm: 9
}
//Define what we want to buy
var wanted = "ice_cream";
//Prevent accessing undefined price
if(prices[wanted]==null)
  throw new Error("We don't have this thing in stock yet!");
//Divide the cash by the price to know the amount
var amount = Math.floor(gold/prices[wanted]);  
//And calculate spare cash - modulo calculates rest after division
var spare = gold%prices[wanted]
//Finally find out how many charms can be bought with the rest gold
var charms = Math.floor(spare/prices.charm);
//and let the user know
alert("With "+gold+" gold, you can buy "+amount+" "+wanted+"(s) and "+charms+" charms.");

You can run this code without HTML. 您可以不使用HTML的情况下运行此代码

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

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