Skip to content

Commit 70c3018

Browse files
committed
1-Key-errors are done.
1 parent b31a586 commit 70c3018

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

Sprint-2/1-key-errors/0.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// I predict the code will give an error because inside the function we create a variable called str again.
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

@@ -9,5 +9,10 @@ function capitalise(str) {
99
return str;
1010
}
1111

12+
console.log(capitalise("hello"));
1213
// =============> write your explanation here
14+
// The error occurs because we are trying to declare a variable named str inside the function, but it is already declared as a parameter. This creates a conflict and results in a syntax error.
1315
// =============> write your new code here
16+
function capitalise(str) {
17+
return `${str[0].toUpperCase()}${str.slice(1)}`;
18+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5-
5+
// console.log(decimalNumber); will cause error because decimalNumber only exists inside the function.
66
// Try playing computer with the example to work out what is going on
77

88
function convertToPercentage(decimalNumber) {
@@ -15,6 +15,14 @@ function convertToPercentage(decimalNumber) {
1515
console.log(decimalNumber);
1616

1717
// =============> write your explanation here
18-
18+
//The error happens because the function already has a parameter called decimalNumber.
19+
//Creating another variable with the same name inside the function causes a conflict
1920
// Finally, correct the code to fix the problem
2021
// =============> write your new code here
22+
function convertToPercentage(decimalNumber) {
23+
const percentage = `${decimalNumber * 100}%`;
24+
25+
return percentage;
26+
}
27+
28+
console.log(convertToPercentage(0.5));

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
// it is syntax error. 3 is a value not a variable name.
78

89
function square(3) {
910
return num * num;
1011
}
1112

1213
// =============> write the error message here
14+
//function square(3) {
15+
^
1316

17+
//SyntaxError: Unexpected number
1418
// =============> explain this error message here
19+
//The error is as I predicted. The function parameter should be a variable name, not a value.
20+
//In this case, 3 is a value and cannot be used as a parameter name.
1521

1622
// Finally, correct the code to fix the problem
1723

1824
// =============> write your new code here
1925

26+
function square(num) {
27+
return num * num;
28+
}
2029

0 commit comments

Comments
 (0)