Skip to content

Commit 11ee71c

Browse files
committed
Sprint 2 redone
1 parent 1d8c408 commit 11ee71c

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

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

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

3+
34
// Predict the output of the following code:
4-
// =============> Write your prediction here. It may run but give incorrect results because of const num & no parameter specified
5+
// =============> Write your prediction here.
6+
// //ANS: To return the last digit of num as a string.
57

68

79
const num = 103;
@@ -20,15 +22,31 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2022
// The last digit of 105 is 3
2123
// The last digit of 806 is 3
2224
// Explain why the output is the way it is
23-
// =============> write your explanation here. num is a constant variable, it's value cannot change.
25+
// =============> write your explanation here.
26+
// //ANS:
27+
// num is a constant variable, a hardcoded reference.
28+
//The function getLastDigit is currently ignoring the arguments you pass to it(42, 105, 806) because of two main issues:
29+
30+
//Missing Parameter: The function definition function getLastDigit() does not have a parameter inside the parentheses.In JavaScript, if you pass an argument to a function that hasn't defined a parameter to receive it, the function simply ignores that input.
31+
32+
//Scope and Variable Reference: Inside the function, you are explicitly calling num.toString().Since num isn't defined inside the function, the engine looks at the Global Scope, finds const num = 103, and uses that value every single time the function runs.
33+
2434
// Finally, correct the code to fix the problem
2535
// =============> write your new code here
26-
function getLastDigit() {
27-
return num.toString().slice(-1);
36+
const num = 103; // This is now ignored by the function
37+
38+
// We add 'n' as a parameter to "catch" the numbers we pass in
39+
function getLastDigit(n) {
40+
// We call toString() on 'n' instead of the global 'num'
41+
return n.toString().slice(-1);
2842
}
43+
44+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
45+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
46+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
47+
2948
// This program should tell the user the last digit of each number.
3049
// Explain why getLastDigit is not working properly - correct the problem
31-
// getLastDigit is not working properly because the value for num is fixed at '103'
32-
function getLastDigit(num) {
33-
return num.toString().slice(-1);
34-
}
50+
//ANS: Done above.
51+
// ANS:getLastDigit was not working properly because the value for num is fixed at '103'
52+

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
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-
return Math.round((weight) / (height ** 2)).toFixed(1)
20-
}
18+
// BMI = kg / m^2
19+
const bmi = weight / (height * height);
20+
21+
// .toFixed(1) rounds to 1 decimal place and returns a string
22+
return bmi.toFixed(1);
23+
}
24+
25+
// Example usage:
26+
const myBMI = calculateBMI(70, 1.73);
27+
console.log(`The calculated BMI is: ${myBMI}`); // Output: "23.4"

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
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-
function upCase(strCar) {
18-
let result = strCar.toUpperCase().replaceAll(" ", "_");
19-
return result;
17+
18+
19+
function toUpperSnakeCase(text) {
20+
// 1. replaceAll(' ', '_') swaps every space for an underscore
21+
// 2. toUpperCase() converts the letters to all caps
22+
return text.replaceAll(' ', '_').toUpperCase();
2023
}
24+
25+
// Examples:
26+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
27+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
28+
console.log(toUpperSnakeCase("javascript is fun")); // "JAVASCRIPT_IS_FUN"

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,39 @@
55

66
// You should call this function a number of times to check it works for different inputs
77

8-
function toPounds(money) {
9-
const moneyWithoutTrailingP = money.substring(
8+
/**
9+
* Converts a pence string (e.g., "399p") into a formatted pound string (e.g., "£3.99")
10+
* @param {string} penceString - The input string ending in 'p'
11+
*/
12+
function toPounds(penceString) {
13+
// 1. Remove the trailing 'p'
14+
const penceStringWithoutTrailingP = penceString.substring(
1015
0,
11-
money.length - 1
16+
penceString.length - 1
1217
);
13-
const paddedPenceNumberString = moneyWithoutTrailingP.padStart(3, "0");
18+
19+
// 2. Pad with leading zeros to ensure at least 3 characters (e.g., "5p" -> "005")
20+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
21+
22+
// 3. Extract the pounds (everything except the last two digits)
1423
const pounds = paddedPenceNumberString.substring(
1524
0,
1625
paddedPenceNumberString.length - 2
1726
);
27+
28+
// 4. Extract the pence (the last two digits)
1829
const pence = paddedPenceNumberString
1930
.substring(paddedPenceNumberString.length - 2)
2031
.padEnd(2, "0");
21-
result = pounds + "." + pence;
22-
res = ${pounds}.${pence}`;
23-
return res;
32+
33+
// 5. Return the formatted string
34+
return ${pounds}.${pence}`;
2435
}
25-
console.log(toPounds("399p"))
26-
console.log(toPounds("1099p"))
36+
37+
// --- Testing the function with different inputs ---
38+
39+
console.log(toPounds("399p")); // Expected: £3.99
40+
console.log(toPounds("50p")); // Expected: £0.50
41+
console.log(toPounds("1250p")); // Expected: £12.50
42+
console.log(toPounds("8p")); // Expected: £0.08
43+
console.log(toPounds("1002p")); // Expected: £10.02

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) {
1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here.pad will be called three times.
20+
// =============> write your answer here.
21+
// //ANS: 3 times.
2122

2223
// Call formatTimeDisplay with an input of 61, now answer the following:
2324

2425
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here. the value assigned to num when pad is called for the first time is 00
26+
// =============> write your answer here.
27+
// ANS: the value assigned to num when pad is called for the first time is 0
2628

2729
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here. the return value of pad is called for the first time is '00'
30+
// =============> write your answer here.
31+
// //ANS: the return value of pad is called for the first time is '00'
2932

3033
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here. the value assigned to num when pad is called for the last time is 01
34+
// =============> write your answer here.
35+
// ANS: the value assigned to num when pad is called for the last time is 1
3236

3337
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here. the return value assigned to num when pad is called for the last time in this program is '01'
38+
// =============> write your answer here.
39+
// ANS: Sthe return value assigned to num when pad is called for the last time in this program is '01'

0 commit comments

Comments
 (0)