generated from Code-the-Dream-School/web-basics-2-week-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (144 loc) · 4.93 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Example Challenge
* Print out "hello world" in the console
*
* Steps:
* 1. Use `console.log` to print out "hello world"
*
* Example Output: "hello world"
*/
console.log('hello world')
/* Challenge #1
* Create a variable and print the value
*
* Steps:
* 1. Create a variable and assign it any value
* 2. Use `console.log` to print out your variable
*
* Example Output: n/a
*/
// TODO: ADD CODE HERE
let myVariable = 1234;
console.log(myVariable);
/* Challenge #2
* Create variables for your first and last name then print your full name
*
* Steps:
* 1. Create a variable called `firstName` and assign your first name as the string value
* 2. Create a variable called `lastName` and assign your last name as the string value
* 3. Use `console.log` to print out your full name (make sure there's a space!)
*
* Example Output: "Elizabeth Thompson"
*/
// TODO: ADD CODE HERE
let firstName = 'Kon';
let lastName = 'Ham';
console.log(firstName + ' ' + lastName);
/* Challenge #3
* Calculate the total price rounded to two decimals
*
* Steps:
* 1. Create a variable called `price` and assign a float value (ex. 9.98)
* 2. Create a variable called `quantity` and assign an integer value (ex. 5)
* 3. Create a variable called `total` and assign the value of `price` multiplied by `quantity`
* 4. Use `console.log` to print out the `total` (rounded to the nearest two decimals)
*
* Example Output: "$49.90"
*/
// TODO: ADD CODE HERE
let price = 9.98;
let quantity = 71;
let priceAndQuantityMult = price * quantity;
let total = priceAndQuantityMult.toFixed(2);
console.log(`$${total}`);
/* Challenge #4
* Declare a variable and conditionally assign it a value
*
* Steps:
* 1. Create a variable called `weather` and assign "Rainy" as the string value
* 2. Declare a variable called `message` and don't assign it a value
* 3. Create a conditional statement that checks if `weather` is equal to "Sunny"
* 4. If `weather` meets the condition above, then set `message` equal to "Yay! It's sunny outside"
* 5. Add an `else` clause that sets `message` equal to "Aww! It's not sunny outside"
* 6. Use `console.log` to print out the value of `message`
*
* Example Output: "Aww! It's not sunny outside"
*/
// TODO: ADD CODE HERE
let weather = 'Rainy';
let message;
if (weather == 'Sunny') {
message = 'Yay! It\'s sunny outside';
} else {
message = 'Aww! It\'s not sunny outside';
}
console.log(message);
/* Challenge #5
* Create a function that returns any string
*
* Steps:
* 1. Create a function called `myFunction` using the function keyword
* 2. Add a return statement in your function which returns a random string
* 3. Use `console.log` to print out the output of `myFunction()`
*
* Example Output: "I called a function!"
*/
// TODO: ADD CODE HERE
function myFunction() {
let randomString = 'This is a random string. But where\'s the yarn?';
return randomString;
}
console.log(myFunction());
/* Challenge #6
* Rewrite the function in Challenge #5 as an arrow function
*
* Steps:
* 1. Create a function called `myArrowFunction` using the arrow syntax
* 2. Add a return statement in your function which returns a random string
* 3. Use `console.log` to print out the output of `myArrowFunction()`
*
* Example Output: "I called an arrow function!"
*/
// TODO: ADD CODE HERE
const myArrowFunction = () => {
let randomString = 'This is a random string. But where\'s the yarn?';
return randomString;
}
console.log(myArrowFunction());
/* Challenge #7
* Create a function that accepts a string argument and returns it in all uppercase
*
* Steps:
* 1. Create a function called `stringToUpper`
* 2. Add a parameter called `str` in your function signature
* 3. Add a return statement in your function which returns the input string in all uppercase letters
* 4. Use `console.log` to print out the output of `stringToUpper(str)`
*
* Example Output: "WHY AM I SCREAMING"
*/
// TODO: ADD CODE HERE
const stringToUpper = (str) => {
return str.toUpperCase();
}
console.log(stringToUpper('when i wear hearing protection i start yelling because i can\'t hear myself.'));
/* Challenge #8
* Create a function that accepts a number argument and returns double its value
*
* Steps:
* 1. Create a function called `multiplyByTwo`
* 2. Add a parameter called `number` in your function signature
* 3. Add a return statement in your function which returns `number` times two
* 4. Create a variable called `num1` and assign it to the result of `multiplyByTwo(1)`
* 5. Create a variable called `num2` and assign it to the result of `multiplyByTwo(5)`
* 6. Create a variable called `num3` and assign it to the result of `multiplyByTwo(10)`
* 7. Use `console.log` to print out the value of `num1`, `num2`, and `num3`
*
* Example Output: 2, 10, 20
*/
// TODO: ADD CODE HERE
const multiplyByTwo = (number) => {
return number * 2;
}
let num1 = multiplyByTwo(1);
let num2 = multiplyByTwo(5);
let num3 = multiplyByTwo(10);
console.log(num1, num2, num3);