1

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.

tbodt
  • 16,609
  • 6
  • 58
  • 83
lonerunner
  • 1,282
  • 6
  • 31
  • 70

1 Answers1

2

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.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Thank you for your detailed explanations. Honestly i didn't expected that someone just give me the code, i started on working something but javascript is still a bit strange to me. I actually expect to someone explain me how to calculate with spare change, that's what is bothering me. I did a bit of simple code where i just divide total price with helmet price and when i get helmet price like 2.43 those 0.43 is unknown to me how to convert as change in charms. – lonerunner Jun 23 '14 at 17:50
  • 1
    Noone but you knows what charms are. In my answer you can see however, how I recalculate the rest value. Every line is commented. I would be very sad if you didn't even read it after I tried so hard to make a nice answer. – Tomáš Zato Jun 23 '14 at 17:52
  • I have read it and tested on jsfiddle and even converted values to fit my needs and basically this is what i need, but from tests your calculations are converting to lovest neared value which leads to 1 charm less calculated. i noticed math floor and from php this rounds it to nearest lower value, ill try with math ceil wich should round it up. I will see to make a working example and post on website in next few hours so you can see how much you helped me. – lonerunner Jun 23 '14 at 18:10
  • This will make it possible to generate money during trade - figure he is going to get 12.3 charms (over 60gold). Rounding it up will grant more gold. – Tomáš Zato Jun 23 '14 at 18:13
  • @AleksandarĐorđević I've put a lot of effort in this answer and I tried to help you. I actually even did the job for you even though I originally didn't want to. Why didn't you still accept the answer after half a year? I can see you were active just yesterday. – Tomáš Zato Mar 06 '15 at 12:07
  • Well if you really want me i will mark this as an answer, but actually it turns out that this is not quite a good and accurate solution when it comes to a bit more complex calculations, when i have few different values and spare. So i ended up with writing calculator in php. – lonerunner Mar 06 '15 at 17:43