Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// Line 3 is reassigning the count value to be the current value of count plus 1, which is also a statement.
3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ let lastName = "Johnson";
// Declare a variable called initials that stores the first character of each string.
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.

let initials = ``;
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn

6 changes: 4 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ console.log(`The base part of ${filePath} is ${base}`);
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;
const dir = filePath.slice(0, lastSlashIndex);
const ext = base.slice(base.lastIndexOf("."));
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
7 changes: 7 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing

// Math.floor - Rounds a number DOWN to the nearest integer
// Math.random - generates a random floating number > 0 and < 1
// (maximum - minimum + 1) = 100 - 1 + 1 = 100
// So, num represents a rounded down random integer generated between 1 and 100 + 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job explaining. 🤔 Is the "+1" at the end of line 14 a typo?
num will be a number between 1-100 (inclusive)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Jenny,

You are right. I was not thinking of even the edge case, as Math.floor(99.99) + 1 = 99 + 1 = 100, has been rounded down. So, the num should have been between 1 and 100 inclusive.
I have updated the answer in 0da267c.


console.log(num);
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
// This is just an instruction for the first activity - but it is just for human consumption
// We don't want the computer to run these 2 lines - how can we solve this problem?
// We can comment them out
2 changes: 1 addition & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
// The variable should be declared before it is used in console.log

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = String(cardNumber).slice(-4);

// A number was not able to be sliced, whereas a string can be sliced
// So, the error is that cardNumber should be a string, not a number
console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";

// Change the above names of variables to avoid errors
18 changes: 17 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -13,10 +13,26 @@ console.log(`The percentage change is ${percentageChange}`);

// a) How many function calls are there in this file? Write down all the lines where a function call is made

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing one function call in your list. Can you find it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, I should have included the console.log function in the list. The console.log function is a built-in function that can also be called. I have updated the answer in 0da267c.


// There are 4 function calls in this file
// Line 4: carPrice.replaceAll(",", "")
// Line 4: Number(carPrice.replaceAll(",", ""))
// Line 5: priceAfterOneYear.replaceAll("," "")
// Line 5: Number(priceAfterOneYear.replaceAll("," ""))

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?

// The error is coming from line 5
// The error is due to a missing comma
// Add a comma between the two quotations

// c) Identify all the lines that are variable reassignment statements

// Line 4 and Line 5

// d) Identify all the lines that are variable declarations

// Line 1, Line 2, Line 7 and Line 8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

// The expression is replacing all the commas in the string carPrice with nothing, and then converting it to a number type
19 changes: 19 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,37 @@ const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);
console.log(remainingSeconds)

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

// a) How many variable declarations are there in this program?

// There are 6 variable declarations

// b) How many function calls are there?

// There is 1 function call

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// % is a remainder operator.
// movieLength % 60 represents the remainder when 8784/60 is calculated, which is 24 seconds

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

// The expression is calculating the total number of minutes in the movie length
// It does this by subtracting the remaining seconds from the total movie length, and then dividing it by 60

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

// The result represents the movie length in hours:minutes:seconds
// A better name could be hoursMinutesSeconds or movieLengthFormatted

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

// The code will only work for positive integers
// if the value is 0 or negative, the code will not make sense
// if the value is a decimal, the code will give float results
// if the value is not a number, the code will give an error
24 changes: 24 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
// 2.1 removes the trailing 'p' from the string by taking a substring from index 0 to the second last character
// 2.2 penceStringWithoutTrailingP now = "399"

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
// 3.1 ensures the string has at least 3 characters by adding zeros to the start if necessary
// 3.2 However, since "399" already has 3 characters, it remains unchanged
// 3.3 paddedPenceNumberString now = "399"

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
// 4.1 taking the first characters of the string up to the last two characters
// 4.2 since "399" has only 3 characters, this only takes the first character
// 4.3 pounds now = "3"

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
// 5.1 takes the last two characters of the string to represent the pence
// 5.2 since "399" has only 3 characters, this takes the last two characters "99"
// 5.3 padEnd(2, "0") ensures that if there were less than 2 characters, it would add a "0" to the end
// 5.4 pence now = "99"

// 6. console.log(`£${pounds}.${pence}`):
// 6.1 constructs a formatted string representing the price in pounds and pence
// 6.2 prints "£3.99" to the console
5 changes: 5 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
The alert function call a small separate window pop out, written "Hello world!". There is a OK button at the bottom right to dismiss it.

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
The prompt function pop out a small window similar to alert but it has a input section where I can type my name.

What is the return value of `prompt`?
The return value of prompt is a string. If we did not enter anything and pressed ok, the value is also a string.
However, if we pressed cancel, it will return null.
10 changes: 10 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
I got ƒ log() { [native code] }
Chrome is telling me console.log a built-in function.

Now enter just `console` in the Console, what output do you get back?
The output is the console object, which has many built-in methods, such as assert, clear, and context.

Try also entering `typeof console`
The type of console is an object.

Answer the following questions:

What does `console` store?
A console contains its properties, and many of its properties are built-in methods that we can call.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
The syntax console.log references to the log method built-in the console object, whereas console.assert references to the assert method.
The log method can output message to the console.
The assert method can output a error message when an assertion is false.
In particular, the "." is a accessing method syntax. As a console is an object which has many built-in methods, the "." can refer to the corresponding method after the ".", such as log and assert.