Skip to content

Commit 83547a2

Browse files
committed
Fixed and updated files in Sprint-2
1 parent 9d2753f commit 83547a2

File tree

7 files changed

+70
-42
lines changed

7 files changed

+70
-42
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
// So, the error sounds "SyntaxError: Identifier 'str' has already been declared", it means that we can't declare the sane variable name twice
1313
// =============> write your new code here
1414
function capitalise(str) {
15-
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
16-
return capitalisedStr;
15+
return `${str[0].toUpperCase()}${str.slice(1)}`;
1716
}
1817

1918
console.log(capitalise("greetings")); // Output: "Greetings"

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
// Finally, correct the code to fix the problem
2525
// =============> write your new code here
2626
function convertToPercentage(decimalNumber) {
27-
const percentage = `${decimalNumber * 100}%`;
28-
29-
return percentage;
27+
return `${decimalNumber * 100}%`;
3028
}
3129

3230
console.log(convertToPercentage(0.5));

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

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

33
// =============> I think the result will be undefined because in the function there is no return statement.
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
5+
// function multiply(a, b) {
6+
// console.log(a * b);
7+
// }
88

99
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

1111
// =============> Its interesting - function log the result but doesn't return it. So when we see the output 320 and "undefined"
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> function multiply(a, b) {
15-
// return a * b;
16-
// }
17-
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
14+
function multiply(a, b) {
15+
return a * b;
16+
}
17+
18+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Predict and explain first...
22
// =============> I guess that the output will be undefined because there is a semicolon after the "return" and this breaks the line of code, and after return statement nothing can be returned
33

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
4+
// function sum(a, b) {
5+
// return;
6+
// a + b;
7+
// }
88

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
9+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> The output is "The sum of 10 and 32 is undefined" as I predicted above
1212
// Finally, correct the code to fix the problem
13-
// =============> function sum(a, b) {
14-
// return a + b;
15-
// }
13+
function sum(a, b) {
14+
return a + b;
15+
}
16+
17+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
// const num = 103;
1111

12-
function getLastDigit() {
13-
return num.toString().slice(-1);
14-
}
12+
// function getLastDigit() {
13+
// return num.toString().slice(-1);
14+
// }
1515

16-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
17-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
18-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
16+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
17+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
18+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1919

2020
// Now run the code and compare the output to your prediction
2121
// =============> The last digit of 42 is 3
@@ -24,13 +24,13 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2424
// Explain why the output is the way it is
2525
// =============> Because we use the global variable "num" in the function instead of passing the parameter to the function.
2626
// Finally, correct the code to fix the problem
27-
//=============> const num = 103;
28-
// function getLastDigit(num) {
29-
// return num.toString().slice(-1);
30-
// }
31-
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32-
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33-
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
27+
28+
function getLastDigit(num) {
29+
return num.toString().slice(-1);
30+
}
31+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
3434
//
3535
// This program should tell the user the last digit of each number.
3636
//

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
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
1717

18-
function toUpperCaseSnakeString(str) {
18+
function toUpperSnakeCase(str) {
1919
transformedStr = str.toUpperCase().replaceAll(" ", "_");
2020
return transformedStr;
2121
}
2222

23-
console.log(toUpperCaseSnakeString("hello there")); // "HELLO_THERE"
24-
console.log(toUpperCaseSnakeString("lord of the rings")); // "LORD_OF_THE_RINGS"
25-
console.log(toUpperCaseSnakeString("code your future is great")); // "CODE_YOUR_FUTURE_IS_GREAT"
23+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
24+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
25+
console.log(toUpperSnakeCase("code your future is great")); // "CODE_YOUR_FUTURE_IS_GREAT"

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
9-
}
10-
return `${time} am`;
6+
const hours24 = Number(time.slice(0, 2));
7+
const minutes = time.slice(3).padStart(2, "0");
8+
const timeSuffix = hours24 >= 12 ? "pm" : "am";
9+
10+
let hours12 = hours24 % 12;
11+
if (hours12 === 0) hours12 = 12;
12+
13+
const formattedHours12 = String(hours12).padStart(2, "0");
14+
15+
return `${formattedHours12}:${minutes} ${timeSuffix}`;
1116
}
1217

1318
const currentOutput = formatAs12HourClock("08:00");
@@ -23,3 +28,26 @@ console.assert(
2328
currentOutput2 === targetOutput2,
2429
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2530
);
31+
32+
const currentOutput3 = formatAs12HourClock("00:00");
33+
const targetOutput3 = "12:00 am";
34+
console.assert(
35+
currentOutput3 === targetOutput3,
36+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
37+
);
38+
39+
const currentOutput4 = formatAs12HourClock("12:00");
40+
const targetOutput4 = "12:00 pm";
41+
console.assert(
42+
currentOutput4 === targetOutput4,
43+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
44+
);
45+
46+
const currentOutput5 = formatAs12HourClock("19:12");
47+
const targetOutput5 = "07:12 pm";
48+
console.assert(
49+
currentOutput5 === targetOutput5,
50+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
51+
);
52+
53+
// I found that when I added minutes, the output became incorrect because the original function only changed the hours. I fixed this bug by introducing a new variable called minutes and using it as the second part of the output after the hours. I also added some formatting for the minutes and defined the period of time (am/pm) in a variable called timeSuffix to make the output display correctly.

0 commit comments

Comments
 (0)