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
5 changes: 5 additions & 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,8 @@ 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 re-assigning the variable "count" with a new value that increases the previous value by 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Operation like count = count + 1 is very common in programming, and there is a programming term describing such operation.

Can you find out what one-word programming term describes the operation on line 3?

*/
5 changes: 5 additions & 0 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ let lastName = "Johnson";
// 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 firstNameInitials = firstName.charAt(0);
let middleNameInitials = middleName.charAt(0);
let lastNameInitials = lastName.charAt(0);
initials= `${firstNameInitials}${middleNameInitials}${lastNameInitials}`
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 lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex);
console.log(`The dir filePath is ${dir} and the ext filePath is ${ext}`);

// https://www.google.com/search?q=slice+mdn
12 changes: 12 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,15 @@ 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


/*
num stores a random integer between minimum (1) and maximum (100), inclusive.

Breakdown:
1. Math.random() generates a decimal number between 0 (inclusive) and 1 (exclusive).
2. Multiplying by (maximum - minimum + 1) scales the range to 0–100.
3. Math.floor() removes the decimal part, producing integers from 0–99.
4. Adding minimum shifts the range to 1–100.
*/
console.log(num);
23 changes: 21 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
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?

//The error found when running the code-> SyntaxError: Unexpected identifier 'is'.
// syntax errors means the grammar rules of the language has not been applied. The instruction is written in plain english in a js file. It is not written in js syntax so js cannot parse it.

// An identifier just means:
//A variable name
//A function name
// Or any word that isn’t a keyword
// So "is" is being treated like a variable name — and the parser wasn’t expecting one at that position.

// //Solution:
/*In this case, comment out the code by adding two forward slashes to a single line comment or
a single slash and a multiplication sign to both ends of a double line comment like the one used for this comment.
Other solutions could be checking:
Missing quotes
Missing operator
Missing comma
Or misplaced word
*/
4 changes: 4 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

const age = 33;
age = age + 1;

//TypeError: Assignment to constant variable.
//what went wrong
//The program is trying to reassign a constant variable. In javascript a constant once assigned cannot be reassigned another value
4 changes: 4 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

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

//ReferenceError: Cannot access 'cityOfBirth' before initialization
/* A lexical variable was accessed before it was initialized. This happens within any scope (global, module, function, or block) when variables declared with let or const are accessed before the place where they are declared has been executed
*/
10 changes: 9 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().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

//TypeError: cardNumber.slice is not a function
//The Number type value stored in cardNumber does not a slice method
//slice methods only works with arrays, strings or objects that has a callable property named slice

//Solution: Convert the number value to string
//Options: use a String Wrapper or toString method
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
13 changes: 12 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 @@ -12,11 +12,22 @@ 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
// Line 4 and 4 has two function calls Number() and replaceAll()
// - line 10 has a function console.log()

// 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?
// Line 5-
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
// ^^^
// SyntaxError: missing ) after argument list
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you give a more specific reason why the error occurs? (You didn't fix the error by adding a ). )


// c) Identify all the lines that are variable reassignment statements
// carPrice an priceAfterOneYear - lines 4 and 5

// d) Identify all the lines that are variable declarations
//Lines - 1, 2, 7, 8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// replaceAll() removes all occurrence of commas from the string e.g "10,000" becomes "10000" and the Number() wrapper converts
// the string to number type; "10000" becomes 10000

16 changes: 14 additions & 2 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 9; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
const result = `${totalHours}:${String(remainingMinutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
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?
// 6

// b) How many function calls are there?
// 1

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
//Remainder operator.
//The remainder (%) operator returns the remainder left over when one operand is divided by a second operand.
// It always takes the sign of the dividend.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The value stored in totalMinute is calculating the number of minutes that have elapsed in the movie by subtracting the remaining time from the total duration and converting seconds to minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
//The variable result represents a formatted time string in the format HH:MM:SS.
// A better name would be "movieDurationDisplay", as these names more clearly describe the purpose and content of the variable.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// No. Not for all possible values of movieLength, some EdgeCases are:
//1. when movieLength is of negative value, the maths produces negative time and its illogical for a movie duration
//2. if movieLength is not a number, its produces "NAN" and this breaks mathematically
//3. what if movieLength is less than 10? if movieLength is let say 9 , without proper formatting it will look like this 0:0:9 but with 0 padding it looks better like this 0:00:09
9 changes: 8 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
Expand All @@ -24,4 +25,10 @@ console.log(`£${pounds}.${pence}`);
// 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"
/* 1. const penceString = "399p": initializes a string variable with the value "399p"
2. const penceStringWithoutTrailingP = This stores the substring of penceString by extracting all characters except the final p. It takes the substring from index 0 up to (but not including) the last character p, so "399p" becomes "399". This removes the unit symbol so only the numeric portion remains.
3. const paddedPenceNumberString= This stores the value of penceStringWithoutTrailingP, padded to a total length of 3 characters by adding leading zeros where necessary. This guarantees there are always enough digits to separate pounds and pence correctly.
4. const pounds= Stores the substring of paddedPenceNumberString from index 0 to the last 2 index. These leading digits represent the pound portion of the amount.
5. const pence= Extracts the final two digits of the padded string to represent the pence portion. padEnd(2, "0") ensures that the pence value always contains exactly two digits.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we expect this program to work as intended for any valid penceString if we deleted .padEnd(2, "0") from the code?
In other words, do we really need .padEnd(2, "0") in this script?

6. console.log(`£${pounds}.${pence}`); logs the value of pounds and pence into a formatted currency string using template literals
*/
10 changes: 7 additions & 3 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ Let's try an example.

In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;
alert("Hello world!");

What effect does calling the `alert` function have?
What effect does calling the `alert` function have?
A popup modal appears with the String -> "Hello world!"

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`.
let myName = prompt("What is your name?");

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

What effect does calling the `prompt` function have?A modal appears with the "What is your name?" with an input field to enter your name. The input is then stored in the variable myName
What is the return value of `prompt`? My name stored in the variable myName
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the question is asking what return value can the caller expect from the function prompt()

  • When the user clicks the OK button
  • When the user clicks the Cancel button