Skip to content

Commit d9a30d6

Browse files
committed
add my answers to 3-mandatory-interpret/3-to-pounds.js
1 parent 64eba1c commit d9a30d6

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

Sprint-1/3-mandatory-interpret/2-time-format.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ console.log(result);
2222
// c) Using documentation, explain what the expression movieLength % 60 represents
2323
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2424

25-
// Martin answer - the expression movieLength % 60 uses the modulus operator which extracts the value remaining seconds after movieLength is divided by 60
25+
// Martin answer - the expression movieLength % 60 uses the modulo operator which extracts the value remaining seconds after movieLength is divided by 60
2626

2727
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2828

2929
// Martin answer - the expression totalMinutes = (movieLength - remainingSeconds) / 60 subtracts remaining seconds from movieLength (total length of movie in seconds) and divides the result by 60 (representing seconds in one minute) to return the total number of minutes
3030

3131
// e) What do you think the variable result represents? Can you think of a better name for this variable?
3232

33-
// Martin answer - result represents a formatted express of the movie duration as hours, minutes and seconds. A better name for this variable is formattedMovie
33+
// Martin answer - result represents a formatted express of the movie duration as hours, minutes and seconds. A better name for this variable is formattedMovieDuration
3434

3535
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
3636

3737
/* - Martin answer - it works for numerical numbers I have tried, positive and negative, but in the real world you would want to prevent entry of negative numbers.
38-
I have tried adding trailing zeros (8784.00), but JavaScript ignores them.
38+
I have tried adding trailing zeros (8784.00), but JavaScript automatically converts them to a number - 8784.
3939
I have tried converting it to a string ("8784" or "8784.00"), but I get the same results as JavaScript automatically converts them to a number.
4040
Enter text returns all values as NaN - NaN:NaN:NaN
4141

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
66
);
77

88
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9+
910
const pounds = paddedPenceNumberString.substring(
1011
0,
1112
paddedPenceNumberString.length - 2
@@ -25,3 +26,17 @@ console.log(`£${pounds}.${pence}`);
2526

2627
// To begin, we can start with
2728
// 1. const penceString = "399p": initialises a string variable with the value "399p"
29+
30+
// MARTIN ANSWERS BELOW
31+
/*
32+
1. const penceString = "399p" - initialises a string variable with the value of "399p"
33+
2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1) - removes the trailing p so that the value of penceStringWithoutTrailingP is "399"
34+
3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") pads the value to a minimum of 3 spaces, with 0 being added if the value is less than 3 characters. In this case holding there are 3 characters - "399".
35+
4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); - uses the substring method to extract the first value from paddedPenceNumberString and assign it to the variable pounds. In this case the first value is "3"
36+
5. const pence = paddedPenceNumberString
37+
.substring(paddedPenceNumberString.length - 2)
38+
.padEnd(2, "0");
39+
- this uses the substring method to extract the last 2 values in paddedPenceNumberString, followed immediately with the padEnd method which adds zeros if the number of characters is less than 2 spaces. In this case, the value of "99" is assigned to the pence variable
40+
41+
6. console.log(`£${pounds}.${pence}`) is a log of the variables pounds and pence in a formatted string which will print the value - "£3.99"
42+
*/

0 commit comments

Comments
 (0)