-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter05.js
130 lines (93 loc) · 3.53 KB
/
chapter05.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
// ## Coding time!
// ### Improved hello
// Complete the following program so that it asks the user for his first and last names, then show the result of the `sayHello()` function.
// ```js
//Say hello to the user
function sayHello(firstName, lastName) {
const message = `Hello, ${firstName} ${lastName}!`;
return message;
}
// TODO: ask user for first and last name
// TODO: call sayHello() and show its result
// ```
const firstName = prompt('Enter your first name: ')
const lastName = prompt('Enter your last name: ')
sayHello(firstName, lastName);
// ### Number squaring
// Complete the following program so that the `square1()` and `square2()` functions work properly.
// ```js
// Square the given number x
function square1(x) {
// TODO: complete the function code
return Math.pow(x,2);
}
// // Square the given number x
const square2 = x => Math.pow(x,2);// TODO: complete the function code
console.log(square1(0)); // Must show 0
console.log(square1(2)); // Must show 4
console.log(square1(5)); // Must show 25
console.log(square2(0)); // Must show 0
console.log(square2(2)); // Must show 4
console.log(square2(5)); // Must show 25
// ```
// When it's done, update the program so that it shows the square of every number between 0 and 10.
for(i = 0; i <= 10; i++){
console.log(square1(i));
}
// !!! info
// Writing 10 dumb calls to `square()` is forbidden! You know how to repeat statements, don’t you? ;)
// ### Minimum of two numbers
// Let's pretend the JavaScript `Math.min()` function doesn’t exist. Complete the following program so that the `min()` function returns the minimum of its two received numbers.
// ```js
// // TODO: write the min() function
function min(n1,n2){
if(n1 > n2){
return n2
} else {
return n1
}
}
console.log(min(4.5, 5)); // Must show 4.5
console.log(min(19, 9)); // Must show 9
console.log(min(1, 1)); // Must show 1
// ```
// ### Calculator
// Complete the following program so that it offers the four basic arithmetical operations: addition, subtraction, multiplication and division. You can use either a function declaration or a function expression.
// ```js
// // TODO: complete program
function calculate(n1, operator, n2){
switch(operator){
case '+':
return n1 + n2;
case '-':
return n1 - n2;
case '*':
return n1 * n2;
case '/':
return n1 / n2;
}
}
console.log(calculate(4, "+", 6)); // Must show 10
console.log(calculate(4, "-", 6)); // Must show -2
console.log(calculate(2, "*", 0)); // Must show 0
console.log(calculate(12, "/", 0)); // Must show Infinity
// ```
// ### Circumference and area of a circle
// Write a program containing two functions to calculate the circumference and area of a circle defined by its radius. Test it using user input.
function calculateCircumference(radius) {
return 2*Math.PI* radius;
}
function calcuclateAreaOfCircle(radius) {
return Math.PI * Math.pow(radius,2);
}
const radius = parseFloat(prompt('Enter the radius of the circle: '));
calculateCircumference(radius)
calcuclateAreaOfCircle(radius)
// Here are some tips for solving this exercise:
// * Circumference and area calculation formulas should be part of your secondary school memories... Or a Google click away :)
// * The value of number π (Pi) is obtained with `Math.PI` in JavaScript.
// * You might want to use the [exponentiation operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators) `**` to perform computations.
// ```js
// console.log(2 ** 3); // 8: 2 * 2 * 2
// console.log(3 ** 2); // 9: 3 * 3
// ```