forked from faisal2410/js_basic_ostad_b3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_conditionals.js
100 lines (72 loc) · 3.54 KB
/
12_conditionals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// In JavaScript, conditionals are used to execute certain code blocks based on whether a certain condition is true or false.The most common types of conditionals in JavaScript are if statements, if-else statements, and switch statements.
// if statements
// if statements are used to execute a block of code if a certain condition is true.
// let num = 5;
// if (num > 0) {
// console.log("The number is positive");
// }
// In this example, if the variable num is greater than 0, then the console will log the string "The number is positive".
// if -else statements
// if-else statements are used to execute a block of code if a certain condition is true, and a different block of code if that condition is false.
// let num = -5;
// if (num > 0) {
// console.log("The number is positive");
// } else {
// console.log("The number is not positive");
// }
// In this example, if the variable num is greater than 0, then the console will log the string "The number is positive".If the variable num is not greater than 0, then the console will log the string "The number is not positive".
// else -if statements
// else -if statements are used to execute a block of code if a certain condition is true, and a different block of code if that condition is false, and another condition is true.
let num = 0;
if (num > 0) {
console.log("The number is positive");
} else if (num < 0) {
console.log("The number is negative");
} else {
console.log("The number is zero");
}
// In this example, if the variable num is greater than 0, then the console will log the string "The number is positive".If the variable num is not greater than 0, but is less than 0, then the console will log the string "The number is negative".If the variable num is neither greater than 0 nor less than 0, then the console will log the string "The number is zero".
// switch statements
// switch statements are used to execute a block of code based on the value of a variable.
let dayOfWeek = "Monday";
switch (dayOfWeek) {
case "Monday":
console.log("Today is Monday");
break;
case "Tuesday":
console.log("Today is Tuesday");
break;
case "Wednesday":
console.log("Today is Wednesday");
break;
case "Thursday":
console.log("Today is Thursday");
break;
case "Friday":
console.log("Today is Friday");
break;
default:
console.log("It's the weekend!");
break;
}
// In this example, if the variable dayOfWeek is "Monday", then the console will log the string "Today is Monday".If the variable dayOfWeek is "Tuesday", then the console will log the string "Today is Tuesday".The default case
// Question:
// Write a JavaScript function named checkNumber that takes in a single parameter called num.The function should check if num is positive, negative or zero, and return a corresponding message as follows:
// If num is positive, return "The number is positive"
// If num is negative, return "The number is negative"
// If num is zero, return "The number is zero"
// Your task is to implement this function using a conditional statement in JavaScript.
Answer:
function checkNumber(num) {
if (num > 0) {
return "The number is positive";
} else if (num < 0) {
return "The number is negative";
} else {
return "The number is zero";
}
}
// Example usage:
console.log(checkNumber(5)); // Output: "The number is positive"
console.log(checkNumber(-5)); // Output: "The number is negative"
console.log(checkNumber(0)); // Output: "The number is zero"