简体   繁体   中英

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.

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...

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

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.

Start by learning what the javascript is (and what it isn't ). (by this sentence I originally refer to the fact that the question was tagged both Java and 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:

To let the user input numbers, create HTML input elements (outside of the script):

<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. Let's create 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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