-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals_mini_exercises.html
More file actions
57 lines (49 loc) · 1.35 KB
/
conditionals_mini_exercises.html
File metadata and controls
57 lines (49 loc) · 1.35 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>"Use Strict";
// 1. Create conditional logic to represent the following scenario...
// If the car is locked, alert 'will open', otherwise, alert 'will not open
var carIsLocked = true;
if (carIsLocked) {
alert('will not open');
} else {
alert('will open');
}
// 2. Write a function that takes in a string and returns a message based on the string length:
function stringLength(hello) {
if (hello.length === 0) {
alert('Empty string');
} else if (hello.length === 1) {
alert('One character long');
} else if (hello.length === 2) {
alert('Two characters long');
} else {
alert('That\'s a long string!');
}
}
stringLength("hello")
// if the string is no characters, return "Empty string"
// if the string is one character long, return "One character long"
// if the string is two characters long, return "Two characters long"
// Otherwise, return, "That is a long string!"
// 3. Refactor the Following Code into a Ternary Operator:
/*
var message;
var num = 5;
if (num < 10) {
message = 'Num less than 10';
} else {
message = 'Num more than 10';
}
*/
var num = 5;
var message = (num < 10) ? 'Num less than 10' : 'Num more than 10';
console.log(message);
</script>
</body>
</html>