Skip to content

Commit 63e146c

Browse files
committed
conditional statements
1 parent 813ca5f commit 63e146c

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/* 1. Get user input using prompt(“Enter your age:”). If user is 18 or older ,
2+
give feedback:'You are old enough to drive' but if not 18 give another feedback stating to wait for the number of years he needs to turn 18.*/
3+
4+
let yourAge = prompt("Enter your age: ");
5+
if (yourAge >= 18){
6+
console.log(`You're ${yourAge} years old and you're old enough to drive.`);
7+
}
8+
else{
9+
const _age = (18 - yourAge);
10+
console.log(`You're left with ${_age} years to drive.`);
11+
}
12+
13+
/* 2. Compare the values of myAge and yourAge using if … else. Based on the comparison and log
14+
the result to console stating who is older (me or you). Use prompt(“Enter your age:”) to get the age as input.*/
15+
16+
let myAge = prompt("Enter my age: ");
17+
let _yourAge = prompt("Enter your age: ");
18+
19+
const newAge = (myAge - _yourAge);
20+
const newAge2 = (_yourAge - myAge);
21+
22+
if (myAge > _yourAge){
23+
console.log(`I am ${newAge} years older than you`);
24+
}
25+
else{
26+
console.log(`You are ${newAge2} years older than me`);
27+
}
28+
29+
/* 3. If a is greater than b return 'a is greater than b' else 'a is less than b'. Try to implement it in to ways
30+
using if else
31+
ternary operator.*/
32+
let a = prompt("Enter the value for a: ");
33+
let b = prompt("Enter the value for b: ");
34+
if (a > b){
35+
console.log(`${a} is greater than ${b}`);
36+
}
37+
else{
38+
console.log(`${b} is greater than ${a}`);
39+
}
40+
41+
// 4. Even numbers are divisible by 2 and the remainder is zero. How do you check, if a number is even or not using JavaScript?
42+
let num = prompt("Enter a number: ");
43+
44+
if (num % 2 == 0){
45+
console.log(`${num} is an even number`);
46+
}
47+
else{
48+
console.log(`${num} is an odd number`);
49+
}
50+
51+
/* 5. Write a code which can give grades to students according to theirs scores:
52+
80-100, A
53+
70-89, B
54+
60-69, C
55+
50-59, D
56+
0-49, F*/
57+
let score = prompt("Enter your score: ");
58+
if (score >= 80 && score <= 100 ){
59+
console.log(`You achieved grade A`);
60+
}else if(score >= 70 && score <= 79){
61+
console.log(`You achieved grade B`);
62+
}else if(score >= 60 && score <= 69){
63+
console.log(`You achieved grade C`);
64+
}else if(score >= 50 && score <= 59){
65+
console.log(`You achieved grade D`);
66+
}else{
67+
console.log(`You achieved grade F`);
68+
}
69+
70+
/* 6. Check if the season is Autumn, Winter, Spring or Summer. If the user input is :
71+
September, October or November, the season is Autumn.
72+
December, January or February, the season is Winter.
73+
March, April or May, the season is Spring
74+
June, July or August, the season is Summer*/
75+
let month = prompt("Enter month: ");
76+
77+
if (month == 'September' || month == 'October' || month == 'November'){
78+
console.log("The season is Autumn");
79+
}else if (month == 'December' || month == 'January' || month == 'February'){
80+
console.log("The season is Winter");
81+
}else if (month == 'March' || month == 'April' || month == 'May'){
82+
console.log("The season is Spring");
83+
}else{
84+
console.log("The season is summer");
85+
}
86+
87+
// 7. Check if a day is weekend day or a working day. Your script will take day as an input.
88+
let day = prompt('What day is today? ');
89+
90+
switch (day){
91+
case "Sunday":
92+
console.log(`${day} is a weekend`);
93+
break;
94+
case "Monday":
95+
console.log(`${day} is a working day`);
96+
break;
97+
case "Tuesday":
98+
console.log(`${day} is a working day`);
99+
break;
100+
case "Wednesday":
101+
console.log(`${day} is a working day`);
102+
break;
103+
case "Thursday":
104+
console.log(`${day} is a working day`);
105+
break;
106+
case "Friday":
107+
console.log(`${day} is a working day`);
108+
break;
109+
case "Tuesday":
110+
console.log(`${day} is a weekend`);
111+
break;
112+
}
113+
114+
// 8. Write a program which tells the number of days in a month.
115+
let _month = prompt('Enter a month: ')
116+
117+
switch (_month){
118+
case 'January':
119+
console.log(`${_month} has 31 days.`);
120+
break;
121+
case 'February':
122+
console.log(`${_month} has 28/29 days.`);
123+
break;
124+
case 'March':
125+
console.log(`${_month} has 31 days.`);
126+
break;
127+
case 'April':
128+
console.log(`${_month} has 30 days.`);
129+
break;
130+
case 'May':
131+
console.log(`${_month} has 31 days.`);
132+
break;
133+
case 'June':
134+
console.log(`${_month} has 30 days.`);
135+
break;
136+
case 'July':
137+
console.log(`${_month} has 31 days.`);
138+
break;
139+
case 'August':
140+
console.log(`${_month} has 31 days.`);
141+
break;
142+
case 'September':
143+
console.log(`${_month} has 30 days.`);
144+
break;
145+
case 'October':
146+
console.log(`${_month} has 31 days.`);
147+
break;
148+
case 'November':
149+
console.log(`${_month} has 30 days.`);
150+
break;
151+
case 'December':
152+
console.log(`${_month} has 31 days.`);
153+
break;
154+
}

04-Conditional-Statements/index.htm

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Conditional Statement</title>
8+
</head>
9+
<body>
10+
<h1>Conditional Statement</h1>
11+
<p>Introduces conditional statements used in JavaScript.</p>
12+
<ul>
13+
<li>if</li>
14+
<li>if else</li>
15+
<li>switch</li>
16+
</ul>
17+
18+
<script src="conditionals.js"></script>
19+
</body>
20+
</html>
21+
22+

0 commit comments

Comments
 (0)