-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.8.1_Conditionalbranching_Tasks.html
97 lines (91 loc) · 3.12 KB
/
1.8.1_Conditionalbranching_Tasks.html
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
<!DOCTYPE html>
<html>
<head>
<script>
/* Task 1
if (a string with zero)
importance: 5
Will alert be shown?
*/
if ("0")
{
alert( 'Hello' ); // yes, as string '0' returns true in if (not empty)
}
/* Task 2
The name of JavaScript
importance: 2
Using the if..else construct, write the code which asks: ‘What is the “official” name of JavaScript?’
If the visitor enters “ECMAScript”, then output “Right!”, otherwise – output: “You don’t know? ECMAScript!”
*/
officialName = prompt('What is the “official” name of JavaScript?');
if (officialName == 'ECMAScript')
{
alert("Right!");
}
else
{
alert("You don't know?\n ECMAScript");
}
/* Task 3
Show the sign
Using if..else, write the code which gets a number via prompt and then shows in alert:
1, if the value is greater than zero,
-1, if less than zero,
0, if equals zero.
In this task we assume that the input is always a number.
*/
let n = prompt('Enter a number:');
if (n > 0)
{
alert(1);
}
else if (n < 0)
{
alert(-1);
}
else
{
alert(0); // here we have an exception if we dont give any input comparison will think it as 0, so it will alert 0
}
/* Task 4
Rewrite 'if' into '?'
importance: 5
Rewrite this if using the conditional operator '?':
let result;
if (a + b < 4) {
result = 'Below';
} else {
result = 'Over';
}
*/
let a = +prompt('Enter value of a:','');
let b = +prompt('Enter value of b:','');
let result;
alert(result = (a + b < 4) ? 'Below' : 'Over');
/* Task 5
Rewrite if..else using multiple ternary operators '?'.
For readability, it’s recommended to split the code into multiple lines.
let message;
if (login == 'Employee')
{
message = 'Hello';
}
else if (login == 'Director')
{
message = 'Greetings';
}
else if (login == '')
{
message = 'No login';
}
else
{ message = '';
}
*/
let message;
let login = prompt("Who is the User?");
alert( message = (login == 'Employee')? 'Hello!':
(login =='Director') ? 'Greetings' :(login=="") ? "No login":'');
</script>
</head>
</html>