Skip to content

Commit 3a261bb

Browse files
made several improvements to the code in Sprint-1, including fixing errors and improving clarity
1 parent d249dd2 commit 3a261bb

7 files changed

Lines changed: 32 additions & 27 deletions

File tree

Sprint-1/1-key-exercises/2-initials.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ let lastName = "Johnson";
88
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
99

1010
console.log(initials);
11-
12-
// https://www.google.com/search?q=get+first+character+of+string+mdn

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
This is just an instruction for the first activity - but it is just for human consumption
2-
We don't want the computer to run these 2 lines - how can we solve this problem?
3-
// by adding a comment symbol in front of the lines, so that the computer ignores them when running the code.
1+
//This is just an instruction for the first activity - but it is just for human consumption
2+
// We don't want the computer to run these 2 lines - how can we solve this problem?
3+
// by adding a comment symbol in front of the lines, so that the computer ignores them when running the code.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
const age = 33;
3+
/* const age = 33;
44
age = age + 1; // this line will cause an error because age is declared as constant and cannot be reassigned.
5-
5+
*/
66
let age = 33;
77
age += 1; // this line will work because age is declared as a variable and can be reassigned.

Sprint-1/2-mandatory-errors/3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ const last4Digits = `${cardNumber.slice(-4)}`;
1212
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
1313
// At first I thought the error would be that last4Digits is not defined, but I learned that the error is actually because the slice method is being called on a number, which doesn't have that method.
1414
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
15-
const last4Digits = cardNumber.toString().slice(-4); // first option
16-
const last4Digits = String(cardNumber).slice(-4); // second option
17-
const last4Digits = `${cardNumber}`.slice(-4); // third option
15+
const last4Digits = cardNumber.toString().slice(-4); // as slice is a string method, we need to convert the number to a string so that we can use the slice method on it. This can be done by using the toString() method or by using String(cardNumber) or by using template literals as shown in the code snippet
16+
const last4Digits = String(cardNumber).slice(-4);
17+
const last4Digits = `${cardNumber}`.slice(-4);

Sprint-1/2-mandatory-errors/4.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
const 12HourClockTime = "8:53pm"; // this will throw an error because the variable name started with a number where it shouldn't be.
2-
const 24hourClockTime = "20:53"; // the same happens here as well.
1+
// const 12HourClockTime = "8:53pm"; // this will throw an error because the variable name started with a number where it shouldn't be.
2+
//const 24hourClockTime = "20:53"; // the same happens here as well.
3+
const hourClockTime12 = "8:53pm"; // this will work because the variable name starts with a letter and not a number.
4+
const hourClockTime24 = "20:53"; // this will work as well because the variable name starts with a letter and not a number.
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const movieLength = 8784; // length of movie in seconds
2-
3-
const remainingSeconds = movieLength % 60;
4-
const totalMinutes = (movieLength - remainingSeconds) / 60;
5-
6-
const remainingMinutes = totalMinutes % 60;
7-
const totalHours = (totalMinutes - remainingMinutes) / 60;
8-
9-
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10-
console.log(result);
1+
function clockFormatter(movieLength) {
2+
const remainingSeconds = movieLength % 60;
3+
const totalMinutes = (movieLength - remainingSeconds) / 60;
4+
const remainingMinutes = totalMinutes % 60;
5+
const totalHours = (totalMinutes - remainingMinutes) / 60;
6+
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
7+
return result;
8+
}
119

1210
// For the piece of code above, read the code and then answer the following questions
1311

@@ -20,15 +18,20 @@ console.log(result);
2018
// c) Using documentation, explain what the expression movieLength % 60 represents
2119
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2220
/* movieLength % 60 represents the remaining seconds when the movie length of 8784 is divided by 60, thus giving the leftover seconds that don't fit into a full minute.
23-
*/
21+
*/
2422

2523
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2624
/* The expression subtracts the leftover seconds from the movie length and divides it by 60 to give the full minutes. Here's a simple formula to understand the logic, movie length = full minutes + leftover seconds
27-
*/
25+
*/
2826

2927
// e) What do you think the variable result represents? Can you think of a better name for this variable?
30-
/* The variable result outputs the time in a formatted time string of hours:minutes:seconds
31-
It could be more clear to give it a better name like duration or movieDuration.
28+
// The variable result outputs the time in a formatted time string of hours:minutes:seconds
29+
// It could be more clear to give it a better name like duration or movieDuration.
3230

3331
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
34-
// I think it works for any positive number of seconds.
32+
console.log(clockFormatter(0));
33+
console.log(clockFormatter(69));
34+
console.log(clockFormatter(15000));
35+
console.log(clockFormatter(-10));
36+
console.log(clockFormatter(20.4));
37+
// yes, it worked for all values of movieLength, including 0, negative numbers, and decimal numbers. The code will work for any number, but the output may not be meaningful for negative or decimal values.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The last 2 digits represent pence.
4242
4343
Everything before that represents pounds.
4444
So we take from the start (0) up to (but not including) the position where the last 2 digits begin.
45+
*/
4546
/* line 14 Extract the pence part and make sure it’s always 2 digits.
4647
4748
length is 3.
@@ -59,3 +60,4 @@ For "99" → stays "99".
5960
Pence must always be shown as two digits (like £3.05, not £3.5).
6061
// To begin, we can start with
6162
// 1. const penceString = "399p": initialises a string variable with the value "399p"
63+
*/

0 commit comments

Comments
 (0)