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

How To Reverse A Number Using JavaScript

vinonaweil

Revenue Diversification Expert
V Rep
0
0
0
Rep
0
V Vouches
0
0
0
Vouches
0
Posts
128
Likes
100
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we are going to learn on How To Reverse A Number Using JavaScript. This program runs that the user will type the desired number then the program will execute the script to reverse the given number from the user. Let's start with:

JavaScript

This script will use to reverse the given number.

  1. <

    script>
  2. var

    reverse_number =

    0

    ;

  3. var

    num_value =

    parseInt(

    prompt(

    "Type your number to Reverse: "

    )

    )

    ;
  4. document.write

    (

    "<br>"

    )

    ;
  5. document.write

    (

    "<h3>How To Reverse A Number Using JavaScript</h3>"

    )

    ;
  6. document.write

    (

    "<b style='color:blue;'>The Original Number is <b style='color:red;'>"

    +

    num_value +

    "</b> .</b><br />"

    )

    ;
  7. while(

    num_value !=

    0

    )
  8. {
  9. reverse_number =

    parseInt(

    reverse_number *

    10

    )

    ;
  10. reverse_number =

    parseInt(

    reverse_number +

    (

    num_value%

    10)

    )

    ;
  11. num_value =

    parseInt(

    num_value/

    10

    )

    ;
  12. }
  13. document.write

    (

    "<b style='color:blue;'>The Reverse Number is <b style='color:red;'>"

    +

    reverse_number +

    "</b> .</b><br>"

    )

    ;
  14. </

    script>

The Complete Code

Copy and paste to your editor to try this simple tutorial. Enjoy coding.

  1. <!DOCTYPE html>

  2. <html

    >

  3. <head

    >
  4. <title

    >

    How To Reverse A Number Using JavaScript</

    title

    >

  5. <style

    type

    =

    "text/css"

    >
  6. body {
  7. width:500px;
  8. margin:auto;
  9. text-align:center;
  10. }
  11. h3 {
  12. color:blue;
  13. }
  14. </

    style

    >
  15. </

    head

    >

  16. <body

    >
  17. <script

    >
  18. var reverse_number =0;

  19. var num_value = parseInt(prompt("Type your number to Reverse: "));
  20. document.write("<br

    >

    ");
  21. document.write("<h3

    >

    How To Reverse A Number Using JavaScript</

    h3

    >

    ");
  22. document.write("<b

    style

    =

    'color:blue;'

    >

    The Original Number is <b

    style

    =

    'color:red;'

    >

    " + num_value + "</

    b

    >

    .</

    b

    ><br

    /

    >

    ");
  23. while( num_value != 0 )
  24. {
  25. reverse_number = parseInt(reverse_number * 10);
  26. reverse_number = parseInt(reverse_number + (num_value%10));
  27. num_value = parseInt(num_value/10);
  28. }
  29. document.write("<b

    style

    =

    'color:blue;'

    >

    The Reverse Number is <b

    style

    =

    'color:red;'

    >

    " + reverse_number + "</

    b

    >

    .</

    b

    ><br

    >

    ");
  30. </

    script

    >
  31. </

    body

    >
  32. </

    html

    >

Hope that this tutorial will help you a lot.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.


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

452,292

323,341

323,350

Top