Skip to content

Commit 39a813f

Browse files
committed
Complete all Key-Errors exercises
1 parent d28242a commit 39a813f

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> If the function will be called, it will print the first letter of the string
3+
// in capital with the rest of the word in lowercase
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

7-
function capitalise(str) {
8+
/* function capitalise(str) {
89
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
910
return str;
11+
} */
12+
13+
// =============> A SyntaxError was thrown because the str was used to name both the function parameter
14+
// and a new variable, which cannot happen in the same scope
15+
// =============>
16+
function capitalise(str) {
17+
return `${str[0].toUpperCase()}${str.slice(1)}`;
1018
}
1119

12-
// =============> write your explanation here
13-
// =============> write your new code here
20+
console.log(capitalise("mother"));

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
// Predict and explain first...
2+
// There will be another syntax error, because the same name is used for both the parameter and the variable
3+
// (decimalNumber)
24

35
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
6+
// =============>
7+
// We cannot redeclare a variable with the same name in the same scope.
58

69
// Try playing computer with the example to work out what is going on
710

8-
function convertToPercentage(decimalNumber) {
11+
/*function convertToPercentage(decimalNumber) {
912
const decimalNumber = 0.5;
1013
const percentage = `${decimalNumber * 100}%`;
1114
1215
return percentage;
1316
}
14-
15-
console.log(decimalNumber);
16-
17+
*/
1718
// =============> write your explanation here
19+
//
1820

1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
return percentage;
26+
}
27+
28+
console.log(convertToPercentage(0.8));

‎Sprint-3/1-key-errors/2.js‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

43
// this function should square any number but instead we're going to get an error
54

65
// =============> write your prediction of the error here
6+
// I predict it will say 'num is not defined'
77

8-
function square(3) {
9-
return num * num;
10-
}
8+
// function square(3) {
9+
// return num * num;
10+
// }
1111

1212
// =============> write the error message here
13-
13+
// SyntaxError: Unexpected number
1414
// =============> explain this error message here
15-
15+
// Instead of a parameter name, the actual value was passed, resulting in an error;
1616
// Finally, correct the code to fix the problem
1717

1818
// =============> write your new code here
1919

20-
20+
function square(num) {
21+
return num * num;
22+
}
23+
console.log(square(6));

0 commit comments

Comments
 (0)