How to Generate Even and Odd Numbers in JavaScript

How to Generate Even an Odd Numbers in JavaScript

Introduction

In this tutorial we will create a How to Generate Even and Odd Numbers in JavaScript. This tutorial purpose is to teach you generate even and odd numbers. This will tackle all the important functionality that automatically generate the even and odd number base on the number you entered. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you needed to generate even and odd numbers. I will give my best to provide you the easiest way of creating this program How to Generate Even an Odd Numbers. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html form and the generated numbers. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Generate Even an Odd Numbers in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-5">
  17. <form>
  18. <div class="form-group">
  19. <label>Enter a number</label>
  20. <input type="number" id="num" class="form-control"/>
  21. </div>
  22. <center><button type="button" onclick="generateNumbers();" class="btn btn-primary">Generate Number</button></center>
  23. </form>
  24. </div>
  25. <br style="clear:both;"/>
  26. <br /w>
  27. <div class="col-md-12">
  28. <center><h1 class="text-success">Numbers</h4></center>
  29. <div class="col-md-6">
  30. <h4 class="text-info">EVEN</h4>
  31. <div id="even"></div>
  32. </div>
  33. <div class="col-md-6">
  34. <h4 class="text-info">ODD</h4>
  35. <div id="odd"></div>
  36. </div>
  37. </div>
  38. </div>
  39. <script src="script.js"></script>
  40. </body>
  41. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will generate the even and odd numbers. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function generateNumbers(){
  2. let number=document.getElementById('num').value;
  3.  
  4. if(number == "" || number <= 0){
  5. alert('Please enter something first');
  6. }else{
  7. let num=parseInt(number);
  8. let even=[];
  9. let odd=[];
  10. for(var i=1; i<=num; i++){
  11. if (i % 2 == 0){
  12. even.push(i);
  13. }else{
  14. odd.push(i);
  15. }
  16.  
  17. }
  18.  
  19. document.getElementById('even').innerHTML=even.join(' ,');
  20. document.getElementById('odd').innerHTML=odd.join(' ,');
  21.  
  22. }
  23. }

In the code above we just simply create one method called generateNumbers(). this function will generate the even and odd numbers base on the entered numbers. This function uses for loop to loop though the numbers and to determine it whether it is even and odd.

Output:

The How to Generate Even and Odd Numbers in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Generate Even and Odd Numbers in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment