Skip to content

Commit 9bfb891

Browse files
committed
completed all tasks in sprint 2
1 parent 9a4536d commit 9bfb891

File tree

6 files changed

+111
-18
lines changed

6 files changed

+111
-18
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Predict the output of the following code:
44
// =============> Write your prediction here
55

6+
//I predict that it will print out the wrong last digit of all numbers.
7+
68
const num = 103;
79

810
function getLastDigit() {
@@ -15,10 +17,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> 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+
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
28+
// The output is the way it is because the function "getLastDigit" is using a global variable "num" which is set to 103. So, regardless of the input passed to the function, it will always return the last digit of 103, which is 3.
29+
2030
// Finally, correct the code to fix the problem
2131
// =============> write your new code here
2232

33+
function getLastDigit(num) {
34+
return num.toString().slice(-1);
35+
}
36+
37+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
38+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
39+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
40+
2341
// This program should tell the user the last digit of each number.
2442
// Explain why getLastDigit is not working properly - correct the problem
43+
44+
// The function "getLastDigit" is not working properly because it is using a global variable "num" instead of accepting and using a function. Adding `num`as a parameter makes the function work with any number passed in.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
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-
}
18+
// return the BMI of someone based off their weight and height
19+
return (weight / (height * height)).toFixed(1);
20+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
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 str.replaceAll(" ", "_").toUpperCase();
20+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,29 @@
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+
function toPounds(penceString) {
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+
16+
const pounds = paddedPenceNumberString.substring(
17+
0,
18+
paddedPenceNumberString.length - 2
19+
);
20+
21+
const pence = paddedPenceNumberString
22+
.substring(paddedPenceNumberString.length - 2)
23+
.padEnd(2, "0");
24+
25+
return ${pounds}.${pence}`;
26+
}
27+
28+
// Call the function a number of times to check it works
29+
console.log(toPounds("399p")); // £3.99
30+
console.log(toPounds("45p")); // £0.45
31+
console.log(toPounds("8p")); // £0.08
32+
console.log(toPounds("1200p")); // £12.00

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,26 @@ function formatTimeDisplay(seconds) {
1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
2121

22+
// pad will be called 3 times because it is used for minutes, hours and seconds in the return string.
23+
2224
// Call formatTimeDisplay with an input of 61, now answer the following:
2325

2426
// b) What is the value assigned to num when pad is called for the first time?
2527
// =============> write your answer here
2628

29+
// 0, The first call is pad(totalHours),and for 61 seconds, totalHours will be 0 because 61 seconds is less than 1 hour.
30+
2731
// c) What is the return value of pad is called for the first time?
2832
// =============> write your answer here
2933

34+
// 00, pad(0) converts 0 to "0" and padStart(2, "0") makes it "00".
35+
3036
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3137
// =============> write your answer here
3238

39+
// 1, the last call is pad(remainingSeconds), and for 61 seconds, remainingSeconds = 61 % 60 = 1.
40+
3341
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3442
// =============> write your answer here
43+
44+
// 01, the last call is pad(1). pad converts 1 to "1", then padStart(2, "0") adds a leading zero, so it returns "01"

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

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,56 @@
22
// Make sure to do the prep before you do the coursework
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

5-
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`;
11-
}
12-
13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
5+
const currentOutput3 = formatAs12HourClock("00:00");
6+
const targetOutput3 = "12:00 am";
157
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
8+
currentOutput3 === targetOutput3,
9+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
1810
);
1911

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
12+
const currentOutput4 = formatAs12HourClock("12:00");
13+
const targetOutput4 = "12:00 pm";
2214
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
15+
currentOutput4 === targetOutput4,
16+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
2517
);
18+
19+
const currentOutput5 = formatAs12HourClock("00:45");
20+
const targetOutput5 = "12:45 am";
21+
console.assert(
22+
currentOutput5 === targetOutput5,
23+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
24+
);
25+
26+
const currentOutput6 = formatAs12HourClock("12:30");
27+
const targetOutput6 = "12:30 pm";
28+
console.assert(
29+
currentOutput6 === targetOutput6,
30+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
31+
);
32+
33+
const currentOutput7 = formatAs12HourClock("13:05");
34+
const targetOutput7 = "01:05 pm";
35+
console.assert(
36+
currentOutput7 === targetOutput7,
37+
`current output: ${currentOutput7}, target output: ${targetOutput7}`
38+
);
39+
40+
// I tested the function with edge cases around midnight and noon.
41+
// I found that the original function handled times like "08:00" and "23:00",
42+
// but it did not correctly handle "00:xx" (midnight times) or "12:xx" (noon times).
43+
44+
// Bugs found:
45+
// - "00:00" returned "00:00 am" instead of "12:00 am"
46+
// - "00:45" returned "00:45 am" instead of "12:45 am"
47+
// - "12:00" returned "12:00 am" instead of "12:00 pm"
48+
49+
// Fix:
50+
// I added separate conditions for:
51+
// - hours === 0 (midnight -> 12:xx am)
52+
// - hours === 12 (noon -> 12:xx pm)
53+
// - hours > 12 (convert to pm by subtracting 12)
54+
// I also kept the minutes using time.slice(2), so the ":MM" part stays the same.
55+
56+
// After fixing, the function correctly formats morning, noon, afternoon,
57+
// evening, and midnight times in 12-hour clock format.

0 commit comments

Comments
 (0)