Skip to content

Commit 6f2abec

Browse files
committed
Completed answers for 2-mandatory-debug/2.js
1 parent 7f1aa21 commit 6f2abec

File tree

9 files changed

+4291
-7
lines changed

9 files changed

+4291
-7
lines changed

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

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

33
// =============> write your prediction here
4-
4+
// It will throw a undefined error
55
function multiply(a, b) {
66
console.log(a * b);
77
}
88

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

1111
// =============> write your explanation here
12+
// The function printed the answer but didn't return it, so when it was used in the template string, it came out as "undefined."
1213

1314
// Finally, correct the code to fix the problem
1415
// =============> write your new code here
16+
function multiply(a, b) {
17+
return a * b;
18+
}
19+
20+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict it will throw a undefined error
34

4-
function sum(a, b) {
5+
/* function sum(a, b) {
56
return;
67
a + b;
78
}
89
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); */
1011

1112
// =============> write your explanation here
13+
// The problem was that the semicolon after return, which made the function stop before adding the numbers.
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum(a, b) {
17+
return a + b; // Once I put a + b on the same line as return, it worked
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
const bmi = weight / (height * height);
20+
return bmi.toFixed(1);
21+
}
22+
23+
console.log(`Your BMI is ${calculateBMI(68, 1.75)}`);
24+
// How it works: function calculateBMI(weight, height)
25+
// Parameters: weight - number, person's weight in kilograms height - number, person's height in meters
26+
// Returns: The Body Mass Index (BMI), rounded to one decimal place
27+

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
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+
const greeting = "Hello there."
18+
19+
console.log(greeting.toUpperCase());

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@
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+
funnction formatPenceToPounds(penceString) {
8+
9+
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
10+
11+
const pounds = paddedPenceNumberString.slice(0, -2);
12+
const pence = paddedPenceNumberString.slice(-2);
13+
14+
return ${pounds}.${pence}`;
15+
}
16+
17+
console.log(formatPenceToPounds("9p"));
18+
console.log(formatPenceToPounds("39p"));
19+
console.log(formatPenceToPounds("399"));

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,36 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13-
13+
console.log(formatTimeDisplay(61));
1414
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1515
// to help you answer these questions
1616

1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
21+
// Three 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?
2526
// =============> write your answer here
27+
/* The first time pad() runs, it's used for the fours part of the time.
28+
Because 61 seconds equals 0 hours, the value given to num is 0 /*
2629
2730
// c) What is the return value of pad is called for the first time?
2831
// =============> write your answer here
2932
33+
/* The first time pad() runs, it's formatting the hours part of the time.
34+
Since the hours value is 0, it turns that into the string "00" by adding a leading zero before returning it /*
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
38+
/* When the function runs with 61 seconds, the last time pad() is called is for the second part of the time. Since 61 seconds has 1 seconds has 1 second left
39+
over after making a full minute, the value passed into pad() is 1. Inside the function, num equals 1, and it gets turned into "01" so the final time shows as 00:01:01 */
3240

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+
/* When pad() is called for the last time, num is 1 because there's 1 second left over.
44+
The function turns that number into the string "01" by adding a leading zero. That "01" is the value that gets returned and used as the seconds part of the final time display (00:01:01) */
45+

eslint.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import js from "@eslint/js";
2+
import globals from "globals";
3+
import { defineConfig } from "eslint/config";
4+
5+
export default defineConfig([
6+
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.browser } },
7+
]);

0 commit comments

Comments
 (0)