Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Practice Exercise 2

Question

Jatin and Shivam have 5 dogs each and dog's age are stored into an array (one array for each)

Create a program to result the dog is adult or puppy.

Criteria :

if age is greater than 3 then dog is adult otherwise, it is puppy.

Sample Data :

Jatin's Dogs : [5,1,3,4,2]

Shivam's Dogs : [2,2,3,1,5]

Hint

Create 2 for Loop from 1 to 5 for each dog's age check their age by using if condition and result the output.

If you are taking Numbers as the input in variable, then to consider it as a number you have to use parseInt function to convert var to Number

Solution:

var jatinDogs = [5,1,3,4,2]; var shivamDogs = [2,2,3,1,5]; var i; for(i=0;i<=4;i++){ if(jatinDogs[i] > 3){ console.log("Jatin's " + parseInt(i+1) +" dog is : Adult"); }else{ console.log("Jatin's " + parseInt(i+1) +" dog is : puppy"); } } for(i=0;i<=4;i++){ if(shivamDogs[i] > 3){ console.log("Shivam's " + parseInt(i+1) +" dog is : Adult"); }else{ console.log("Shivam's " + parseInt(i+1) +" dog is : puppy"); } }

Output

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8d61e03a-bc88-4eaf-8022-d959bf21670d/Untitled.png

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?