Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ let count = 0;
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: first it calculates count + 1 (using the current value of count),
// then the = assignment operator stores that new result back into the count variable.
// So count changes from 0 to 1.
4 changes: 3 additions & 1 deletion Sprint-2/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const 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.

const initials = ``;
const initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;

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

console.log(initials)
8 changes: 5 additions & 3 deletions Sprint-2/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);
console.log(dir);
const dotIndex = base.lastIndexOf(".");
const ext = base.slice(dotIndex);

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
2 changes: 2 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num);

// In this exercise, you will need to work out what num represents?
// 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
// num is a random whole number between 1 and 100
4 changes: 2 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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?
3 changes: 2 additions & 1 deletion Sprint-2/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;
console.log(age);
3 changes: 2 additions & 1 deletion Sprint-2/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 ?

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

5 changes: 4 additions & 1 deletion Sprint-2/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = String(cardNumber).slice(-4);
console.log(last4Digits);


// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Before running the code, make and explain a prediction about why the code won't work
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
//updated expression last4Digits to a String.
8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const clockTime12Hour = "8:53pm";
const clockTime24Hour = "20:53";
console.log(clockTime12Hour);
console.log(clockTime24Hour);

//variable names can not start with numbers
18 changes: 16 additions & 2 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ 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;

console.log(`The percentage change is ${percentageChange}`);
console.log(`The price difference is ${priceDifference}`);


console.log(`The percentage change is ${percentageChange}%`);

// Read the code and then answer the questions below

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

// 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?
// Answer: line 5 {replaceAll(",", ""))} added , to fix the SyntaxError.

// c) Identify all the lines that are variable reassignment statements
// Answer: carPrice = Number(carPrice.replaceAll(",", "")); line 4
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); line 5

// d) Identify all the lines that are variable declarations
// Answer: let carPrice = "10,000"; line1
// let priceAfterOneYear = "8,543"; line 2
// const priceDifference = carPrice - priceAfterOneYear; line 7
// const percentageChange = (priceDifference / carPrice) * 100; line 8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Answer: carPrice.replaceAll(",","") remove commas from the string.
// Number(...) changes the text to a number so subtraction and division work correctly.

20 changes: 19 additions & 1 deletion Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 9325; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -12,14 +12,32 @@ console.log(result);
// 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?
// Answer: 6 variable declarations.

// b) How many function calls are there?
// Answer: 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
// Answer: The % operator is the remainder (modulo) operator.
// movieLength % 60 gives the remainder after dividing the total seconds by 60.
// That remainder is the number of seconds left after removing complete minutes.
// Example: 8784 % 60 === 24.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// Answer: (movieLength - remainingSeconds) / 60
// First it subtracts the leftover seconds so the value divides evenly by 60,
// then it divides by 60 to get the total number of whole minutes in the movie.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// Answer: result stores the movie length formatted as hours:minutes:seconds
// (for this value: "2:26:24").
// A clearer name would be formattedTime.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Answer: It works for non-negative whole numbers of seconds and correctly
// splits time into hours, minutes, and seconds.
// Limitations:
// - fractional values can produce messy decimals
// - negative values do not make sense for a movie length.
// Note: I have changed the movieLength from "8784" to "9325" for testing.
38 changes: 36 additions & 2 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,39 @@ console.log(`£${pounds}.${pence}`);
// You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each step

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

// 1. const penceString = "399p"
// Initialises a string variable with the value "399p"
// (price written in pence, with a trailing "p").

// 2. penceString.substring(0, penceString.length - 1)
// Removes the final character "p".
// For "399p" this becomes "399".
// Stored in penceStringWithoutTrailingP.

// 3. penceStringWithoutTrailingP.padStart(3, "0")
// Ensures the string is at least 3 characters long by padding
// with "0" on the left if needed.
// "399" stays "399".
// Example: "5" would become "005", "42" would become "042".
// This makes it easier to always treat the last 2 digits as pence.

// 4. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2)
// Takes everything except the last 2 characters → the pounds part.
// For "399" this is "3".

// 5. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2)
// Takes only the last 2 characters → the pence part.
// For "399" this is "99".

// 6. .padEnd(2, "0") on the pence part
// Ensures pence is always 2 digits (adds "0" on the right if needed).
// "99" stays "99".
// Example: if pence were "5", it would become "50" with padEnd —
// (note: for money, padStart is usually more common for the numeric part;
// here the code uses padEnd as written).

// 7. console.log(`£${pounds}.${pence}`)
// Prints the final price in pounds format.
// For this input: £3.99
4 changes: 4 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Now try invoking the function `prompt` with a string input of `"What is your nam

What effect does calling the `prompt` function have?
What is the return value of `prompt`?
## Answers
- alert("Hello world!") shows a browser alert dialog with that message.
- prompt("What is your name?") shows an input dialog asking for a name.
- prompt returns the typed string if OK is pressed, or null if Cancel is pressed.
6 changes: 6 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
## Answers
- console.log is a function (native code).
- console is an object containing logging methods.
- typeof console is "object".
- console stores the browser/devtools console API object.
- The dot `.` means property access: console.log is the log property (a function) on the console object.
Loading