Push odd and even numbers to a object

 Push odd and even numbers to a JavaScript object

In this lesson you will learn how you can use for loops and push function to add elements to an object.
It is simple to achieve this using the concepts that we've covered in previous JavaScript lessons.
I'll keep this short, please refer the respective tutorials which are hyperlinked to understand more.

The logic is pretty straight forward. We create an object obj with two properties.
We use a for loop with start and end values and increment it to add elements till the conditions evaluate to true.
Inside the loop we use yet another conditional statement to check if the number is odd or even. 
We are using dot notation to set the value to 'i' inside the loop.

Take a look at the example to understand this.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Push odd and even numbers to a object</title>
    <div id="odd"></div><br>
    <div id="even"></div>
        var odd = document.getElementById("odd"); 
        var even = document.getElementById("even");
        let obj = {
            odd : [],
            even : []
        for(var i=1i<12i++){
            } else{

Post a Comment