Skip to content

Commit 51eedee

Browse files
committed
restore sprint-2 from origin/main
1 parent bc1b331 commit 51eedee

File tree

11 files changed

+19
-309
lines changed

11 files changed

+19
-309
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
// I guess the function is trying to capitalise the first letter of the string,
4-
// capture the rest of the string from index 1 to the end of the string,
5-
// and concatenate them together to return the capitalised string
63

74
// call the function capitalise with a string input
85
// interpret the error message and figure out why an error is occurring
9-
/*
6+
107
function capitalise(str) {
118
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
129
return str;
1310
}
14-
*/
1511

1612
// =============> write your explanation here
17-
// After calling the function capitalise with a string input,
18-
// I got a syntax error that "str" has already been declared.
19-
// This is because str has already been declared as a parameter of the function,
20-
// when a new variable named str, it causes a conflict.
21-
2213
// =============> write your new code here
23-
// I renamed the new variable to capitalisedStr to avoid the conflict
24-
25-
function capitalise(str) {
26-
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
27-
return capitalisedStr;
28-
}
29-
console.log(capitalise("hello")); // should return "Hello"

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
// Predict and explain first...
2-
// At the first glance, I believe the function would occur errors because the variable decimalNumber is declared twice.
3-
// Besides, console.log is trying to log a variable that is not defined in global scope.
42

53
// Why will an error occur when this program runs?
64
// =============> write your prediction here
7-
// The first error should be a syntax error that "decimalNumber" has already been declared.
8-
// Since decimalNumber is declared as a parameter of the function,
9-
// when a new variable also named decimalNumber is declared inside the function,
10-
// it causes a conflict.
11-
12-
// The second error should be a reference error that "decimalNumber is not defined".
135

146
// Try playing computer with the example to work out what is going on
15-
/*
7+
168
function convertToPercentage(decimalNumber) {
179
const decimalNumber = 0.5;
1810
const percentage = `${decimalNumber * 100}%`;
@@ -21,19 +13,8 @@ function convertToPercentage(decimalNumber) {
2113
}
2214

2315
console.log(decimalNumber);
24-
*/
2516

2617
// =============> write your explanation here
27-
// After running the code, I got a syntax error that "decimalNumber" has already been declared.
28-
// I also found out that the decimalNumber inside the function should be declared again.
29-
// Besides, if we want to log the result of the function, we should call the function with an argument inside console.log().
3018

3119
// Finally, correct the code to fix the problem
3220
// =============> write your new code here
33-
function convertToPercentage(decimalNumber) {
34-
const percentage = `${decimalNumber * 100}%`;
35-
36-
return percentage;
37-
}
38-
console.log(convertToPercentage(0.5)); // should return "50%"
39-
console.log(convertToPercentage(0.75)); // should return "75%"

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

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

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

56
// =============> write your prediction of the error here
6-
// I guess the error is a reference error because the parameter is not defined correctly.
7-
/*
7+
88
function square(3) {
99
return num * num;
1010
}
11-
*/
11+
1212
// =============> write the error message here
13-
// function square(3) {
14-
// ^
15-
//SyntaxError: Unexpected number
16-
// In fact, the error is a syntax error because number 3 is unexpected here.
1713

1814
// =============> explain this error message here
19-
// Number 3 is not a valid parameter name, which is misplacing and causes a syntax error.
2015

2116
// Finally, correct the code to fix the problem
2217

2318
// =============> write your new code here
24-
function square(num) {
25-
return num * num;
26-
}
27-
console.log(square(3)); // should return 9
19+
20+

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

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

33
// =============> write your prediction here
4-
// It seems the function multiply did not return any value.
5-
// Therefore, it is like a console.log returning undefined, while it is inside another console.log.
6-
// So the output should be "The result of multiplying 10 and 32 is undefined".
7-
/*
4+
85
function multiply(a, b) {
96
console.log(a * b);
107
}
118

129
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
13-
*/
10+
1411
// =============> write your explanation here
15-
// After running the code, I got a separate output of 320 and "The result of multiplying 10 and 32 is undefined".
16-
// This is because the function multiply does not return any value, so it returns undefined by default.
17-
// The console.log inside the function multiply prints 320 when the function is called.
1812

1913
// Finally, correct the code to fix the problem
2014
// =============> write your new code here
21-
function multiply(a, b) {
22-
return a * b;
23-
}
24-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // should return "The result of multiplying 10 and 32 is 320"

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
// It looks like the function sum return nothing because a+b is written in the next line after return statement.
4-
// Therefore, the output should be "The sum of 10 and 32 is undefined".
53

6-
/*
74
function sum(a, b) {
85
return;
96
a + b;
107
}
118

129
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
13-
*/
14-
// =============> write your explanation here
15-
// After running the code, I got "The sum of 10 and 32 is undefined".
16-
// The return statement ends the function execution and a+b is never evaluated.
1710

11+
// =============> write your explanation here
1812
// Finally, correct the code to fix the problem
1913
// =============> write your new code here
20-
function sum(a, b) {
21-
return a + b;
22-
}
23-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // should return "The sum of 10 and 32 is 42"

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,23 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5-
// The output should always be 3 because the parameter is not defined in the function,
6-
// and the variable num is always 103 in the global scope.
75

86
const num = 103;
9-
/*
7+
108
function getLastDigit() {
119
return num.toString().slice(-1);
1210
}
1311

1412
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1513
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1614
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
17-
*/
15+
1816
// Now run the code and compare the output to your prediction
1917
// =============> write the output here
20-
/*
21-
The last digit of 42 is 3
22-
The last digit of 105 is 3
23-
The last digit of 806 is 3
24-
*/
25-
// As expected, the output is always 3.
26-
2718
// Explain why the output is the way it is
2819
// =============> write your explanation here
29-
//The num is defined in the global scope as 103.
30-
// And the function does not accept any parameters.
31-
// So every time the function is called, it returns the last digit of 103, which is 3.
32-
3320
// Finally, correct the code to fix the problem
3421
// =============> write your new code here
35-
function getLastDigit(num) {
36-
return num.toString().slice(-1);
37-
}
38-
console.log(`The last digit of 42 is ${getLastDigit(42)}`); // should return 2
39-
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // should return 5
40-
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // should return 6
4122

4223
// This program should tell the user the last digit of each number.
4324
// Explain why getLastDigit is not working properly - correct the problem

Sprint-2/3-mandatory-implement/1-bmi.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@
1515
// It should return their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
const bmi = weight / (height * height);
20-
return bmi.toFixed(1);
21-
}
22-
23-
console.log(calculateBMI(70, 1.73)); // should return 23.4
18+
// return the BMI of someone based off their weight and height
19+
}

Sprint-2/3-mandatory-implement/2-cases.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,3 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17-
18-
function toUpperSnakeCase(str) {
19-
// return the string in UPPER_SNAKE_CASE
20-
return str.toUpperCase().replaceAll(" ", "_");
21-
}
22-
23-
console.log(toUpperSnakeCase("hello there")); // should return "HELLO_THERE"
24-
console.log(toUpperSnakeCase("lord of the rings")); // should return "LORD_OF_THE_RINGS"

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,3 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7-
8-
/* Code from Sprint-1 below for reference
9-
const penceString = "399p";
10-
11-
const penceStringWithoutTrailingP = penceString.substring(
12-
0,
13-
penceString.length - 1
14-
);
15-
16-
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17-
const pounds = paddedPenceNumberString.substring(
18-
0,
19-
paddedPenceNumberString.length - 2
20-
);
21-
22-
const pence = paddedPenceNumberString
23-
.substring(paddedPenceNumberString.length - 2)
24-
.padEnd(2, "0");
25-
26-
console.log(`£${pounds}.${pence}`);
27-
*/
28-
29-
function toPounds(penceString) {
30-
if (typeof penceString !== "string" || !penceString.endsWith("p")) {
31-
console.log("Invalid input, please provide a penceString ending with 'p'");
32-
return "Invalid input, please provide a penceString ending with 'p'";
33-
}
34-
35-
const penceStringWithoutTrailingP = penceString.substring(
36-
0,
37-
penceString.length - 1
38-
);
39-
40-
if (Number.isNaN(Number(penceStringWithoutTrailingP))) {
41-
console.log("Invalid input, please provide a numeric penceString");
42-
return "Invalid input, please provide a numeric penceString";
43-
}
44-
45-
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
46-
const pounds = paddedPenceNumberString.substring(
47-
0,
48-
paddedPenceNumberString.length - 2
49-
);
50-
51-
const pence = paddedPenceNumberString
52-
.substring(paddedPenceNumberString.length - 2)
53-
.padEnd(2, "0");
54-
55-
console.log(${pounds}.${pence}`);
56-
return ${pounds}.${pence}`;
57-
}
58-
59-
toPounds("399p");
60-
toPounds("5p");
61-
toPounds("89p");
62-
toPounds("1234p");
63-
toPounds("0p");
64-
toPounds("70000p");
65-
toPounds("abcp");
66-
toPounds(500);
67-
toPounds("abc");
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
let timesToBeCalled = 0;
2-
31
function pad(num) {
4-
timesToBeCalled += 1;
5-
console.log(
6-
` Pad has been called ${timesToBeCalled} times and num is ${num}`
7-
);
8-
console.log(` The return value will be ${num.toString().padStart(2, "0")}`);
92
return num.toString().padStart(2, "0");
103
}
114

@@ -18,36 +11,24 @@ function formatTimeDisplay(seconds) {
1811
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1912
}
2013

21-
console.log(formatTimeDisplay(61));
22-
2314
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
2415
// to help you answer these questions
2516

2617
// Questions
2718

2819
// a) When formatTimeDisplay is called how many times will pad be called?
2920
// =============> write your answer here
30-
// The pad function will be called 3 times when formatTimeDisplay is called.
31-
// 1st: for totalHours
32-
// 2nd: for remainingMinutes
33-
// 3rd: for remainingSeconds
3421

3522
// Call formatTimeDisplay with an input of 61, now answer the following:
3623

3724
// b) What is the value assigned to num when pad is called for the first time?
3825
// =============> write your answer here
39-
// The value assigned to num is 0 when pad is called for the first time because 61 seconds is 0 hours, 1 minute, and 1 second.
4026

4127
// c) What is the return value of pad is called for the first time?
4228
// =============> write your answer here
43-
// The return value of pad is "00" in string format because 0 is padded to 2 digits with leading zeros.
4429

4530
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
4631
// =============> write your answer here
47-
// The value assigned to num is 1 when pad is called for the last time.
48-
// It is because the remaining seconds is 1 second after taken 60 seconds for 1 minute.
4932

5033
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
5134
// =============> write your answer here
52-
// The return value assigned to num is "01" in string format.
53-
// It is because 1 second is padded to 2 digits with a leading zero.

0 commit comments

Comments
 (0)