Skip to content

Commit 3f9358c

Browse files
committed
Complete all mandatory-debug exercises
1 parent d28242a commit 3f9358c

6 files changed

Lines changed: 60 additions & 14 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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
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);
17+
*/
1618

1719
// =============> write your explanation here
20+
// We create a function that takes a decimal number as a parameter, then declare a variable and assign value to it,
21+
// then create a variable named "percentage", the value of which is calculated by calculating the decimal number
22+
// and multiplying it by 100. Finally, we return the output - percentage.
1823

1924
// Finally, correct the code to fix the problem
2025
// =============> write your new code here

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
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+
// I predict it will say 'num is not defined'
78

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

1213
// =============> write the error message here
13-
14+
// SyntaxError: Unexpected number
1415
// =============> explain this error message here
15-
16+
//
1617
// Finally, correct the code to fix the problem
1718

1819
// =============> write your new code here
1920

2021

22+
console.log(square(3));

‎Sprint-3/2-mandatory-debug/0.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Predict and explain first...
22

3-
// =============> write your prediction here
3+
// =============>
4+
// Variables haven't been declared, so it will throw a reference error
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,13 @@ function multiply(a, b) {
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
12-
13+
// It printed 32 because that particular console function is inside the multiply function (local), whereas the other one
14+
// is outside the scope and when we call the multiply function it comes up as undefined because nothing has been returned
15+
// inside of it.
1316
// Finally, correct the code to fix the problem
1417
// =============> write your new code here
18+
19+
/* function multiply(a, b) {
20+
return a * b;
21+
}
22+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); */

‎Sprint-3/2-mandatory-debug/1.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// it will print the following: `The sum of 10 and 32 is undefined'
34

5+
/*
46
function sum(a, b) {
57
return;
68
a + b;
79
}
810
911
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
12+
*/
1013

1114
// =============> write your explanation here
15+
// the function sum doesn't return any value. The arithmetic operation doesn't execute because it's defined after 'return'
1216
// Finally, correct the code to fix the problem
1317
// =============> write your new code here
18+
function sum(a, b) {
19+
return a + b;
20+
}
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

‎Sprint-3/2-mandatory-debug/2.js‎

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5-
6-
const num = 103;
5+
// it will not print the correct last digits
6+
/*const num = 103;
77
88
function getLastDigit() {
99
return num.toString().slice(-1);
@@ -12,13 +12,29 @@ function getLastDigit() {
1212
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1313
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15+
*/
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
/* The last digit of 42 is 3
20+
The last digit of 105 is 3
21+
The last digit of 806 is 3
22+
*/
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
26+
// Because the method uses the num value which is 103.
2027
// Finally, correct the code to fix the problem
2128
// =============> write your new code here
2229

30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
33+
34+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
37+
2338
// This program should tell the user the last digit of each number.
2439
// Explain why getLastDigit is not working properly - correct the problem
40+
// The function getLastDigit wasn't using intended parameters

0 commit comments

Comments
 (0)