• We just launched and are currently in beta. Join us as we build and grow the community.

Javascript - Simple Currency Converter

KennethTejadaKGJL

Meta Breaker
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
102
Likes
22
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Simple Currency Converter using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/
The Main Interface

This code contains the interface of the application. This code will render application and display the form that will be use in converting. To create this just write these block of code inside the text editor and save this as index.html
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  5. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  6. <script

    src

    =

    "converter.js"

    ></

    script

    >
  7. </

    head

    >
  8. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  12. </

    div

    >
  13. </

    nav>
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    Javascript - Simple Currency Converter</

    h3

    >
  17. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    >
  18. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  19. <div

    class

    =

    "col-md-8"

    >
  20. <h4

    >

    Converter</

    h4

    >
  21. <hr

    style

    =

    "border-top:1px groovy #ccc;"

    /

    >
  22. <div

    class

    =

    "form-group"

    >
  23. <select

    onchange

    =

    "Currency(); Calculate()"

    class

    =

    "form-control"

    id

    =

    "converter"

    style

    =

    "width:30%;"

    >
  24. <option

    value

    =

    "Dollar"

    >

    Dollar</

    option

    >
  25. <option

    value

    =

    "Pound"

    >

    Pound</

    option

    >
  26. <option

    value

    =

    "Euro"

    >

    Euro</

    option

    >
  27. <option

    value

    =

    "Yen"

    >

    Yen</

    option

    >
  28. <option

    value

    =

    "Yuan"

    >

    Yuan</

    option

    >
  29. </

    select

    >
  30. <br

    /

    >
  31. <input

    type

    =

    "number"

    oninput=

    "Calculate()"

    class

    =

    "form-control"

    id

    =

    "value1"

    /

    >
  32. </

    div

    >
  33. <br

    /

    ><br

    /

    >
  34. <div

    class

    =

    "form-group"

    >
  35. <label

    >

    Philippine Peso</

    label

    >
  36. <input

    type

    =

    "number"

    class

    =

    "form-control"

    id

    =

    "value2"

    disabled

    =

    "disabled"

    /

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >
  40. </

    body

    >
  41. </

    html

    >

Creating the Script

This code contains the script of the application. This code will convert input value every time you enter some number. To do this just copy write the code as shown below inside the text editor and save it as converter.js
  1. function

    Currency(

    )

    {
  2. y =

    document.getElementById

    (

    "converter"

    )

    .value

    ;
  3. return

    y;
  4. }

  5. function

    Calculate(

    )

    {
  6. y =

    Currency(

    )

    ;

  7. x =

    document.getElementById

    (

    "value1"

    )

    .value

    ;

  8. if

    (

    x ==

    ""

    )

    {
  9. document.getElementById

    (

    "value2"

    )

    .value

    =

    ""

    ;
  10. }

    else

    {
  11. switch

    (

    y)

    {
  12. case

    "Dollar"

    :
  13. document.getElementById

    (

    "value2"

    )

    .value

    =

    x *

    51.89

    ;
  14. break

    ;

  15. case

    "Pound"

    :
  16. document.getElementById

    (

    "value2"

    )

    .value

    =

    x *

    72.39

    ;
  17. break

    ;

  18. case

    "Euro"

    :
  19. document.getElementById

    (

    "value2"

    )

    .value

    =

    x *

    63.84

    ;
  20. break

    ;

  21. case

    "Yen"

    :
  22. document.getElementById

    (

    "value2"

    )

    .value

    =

    x *

    0.49

    ;
  23. break

    ;

  24. case

    "Yuan"

    :
  25. document.getElementById

    (

    "value2"

    )

    .value

    =

    x *

    8.20

    ;
  26. break

    ;
  27. }
  28. }
  29. }

There you have it we successfully created a Simple Currency Converter
using javascript. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top