JavaScript Euro to Dollar Conversion Tutorial
JavaScript Conversions Tutorial
Now that you can create and style a table, lets try conversions. In this example I will be converting Euro to Dollars. I will separate this tutorial into two parts: the first being a simple conversion and the second having the user decide how many Euros they want to convert to Dollars. Lets begin with a simple conversion. Here is the HTML code to begin with.
We first begin by creating the variables euro and dollar. We then give values to these variables by looking up the conversion rate of euro to dollar. The result is then created by making a final variable called result and making that a product of multiplying euro and dollar. Finally the result can be printed out with the document.write code. The result is as follows.
Experiment by changing the value of euro to whatever you would like to find the corresponding dollar value. With that lets move on to a more advanced conversion system.
The following code will prompt the user to enter the amount of euro they would like to convert to dollars. The code will then take the number entered and convert it to dollars. Finally the result will be printed to the out. Take a look at the code:
The code for the HTML is similar to the first conversion we did however the JavaScript code is different:
We begin by creating the euro variable by giving the user a prompt. This provides the user with a popup which will print out the text between the quote marks in the prompt. The dollar variable remains the same as the first conversion example I did. The code can then be simply printed out using the same code as before with the user input of euro being first. The result will be presented as shown:
Comments
Post a Comment