Skip to content
Closed
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
2 changes: 2 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,5 @@ 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

//The right-hand side count + 1 is evaluated first to produce the value 1, then = assigns that results to count and overwrites the previous value of 0.
3 changes: 1 addition & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

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

12 changes: 7 additions & 5 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
// (All spaces in the "" line should be ignored. They are purely for formatting.)

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
const lastSlashIndex = filePath.lastIndexOf("/"); // 44
const base = filePath.slice(lastSlashIndex + 1); // "file.txt"
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);

// https://www.google.com/search?q=slice+mdn
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex + 1);

// https://www.google.com/search?q=slice+mdn
4 changes: 2 additions & 2 deletions Sprint-1/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?
2 changes: 2 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,5 @@

const age = 33;
age = age + 1;

// The code will throw an error because age is declared as a const, which means it cannot be reassigned after its initial assignment. To fix this, age should be declared using let instead of const.
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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}`);

// The error is that the variable cityOfBirth is being used before it is declared and assigned a value. To fix this, the declaration should be before the console.log statement.
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);

//

const last4Digits = cardNumber.toString().slice(-4);

// 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

//Initially, the code will not work because slice is a method that exist on strings and arrays. While the cardNumber is a number, so it will throw an error when trying to slice it

//To fix this, we can convert the cardNumber to a string using the toString() method before applying the slice method.
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";

// A variable name cannot start with a number.
17 changes: 16 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,25 @@ 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

// There are 5 function calls in this file.
// Line 4 : Number() and replaceAll()
// Line 5 : Number() and replaceAll()
// Line 10 : 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?

// The error is on line 5, where there is a syntax error in the replaceAll method call. The second argument is missing quotes.
// To fix it, we need to add quotes around the empty string: replaceAll(",", "").

// 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, 2, 7, 8.

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

// The expression is first calling the replaceAll method on the carPrice string to remove all commas. "10,000" ==> "10000"
// Then it converts the resulting string into a number using the Number() function. "10000" ==> 10000
21 changes: 18 additions & 3 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const movieLength = 8784; // length of movie in seconds

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

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

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);
Expand All @@ -13,13 +13,28 @@ console.log(result);

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

// There are 6 Variable declarations.

// b) How many function calls are there?

// There are 2 function calls. Console.log() and the template literal used.

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

// movieLength % 60 gives the leftover seconds after converting seconds into whole minutes.

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

//It gets the total minutes by removing the leftover seconds and converting the remaining seconds into minutes by dividing by 60.

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

//remainingMinutes is the minutes part in an hours:minutes:seconds display
// It can be named as minutesDisplay.

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

// The code will work for all positive integer values of MovieLength.
// If movieLength is negative, the code will still run but it will give a negative time which doesn't make sense in this context.
// If movieLength is not an integer, it will still run but it may give unexpected results due to the way the modulus operator works with non-integer values.
19 changes: 12 additions & 7 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
const penceString = "399p";

// This creates a string variable that represents a price in pence, with the letter p at the end.

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// This takes the string from the start up to (but not including) the last character, so it removes the trailing "p" and leaves "399"

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

// This makes sure the pence number has at least 3 characters by adding zeros to the front if it’s too short (so "5" would become "005"), and "399" stays "399".
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// This takes everything except the last 2 digits, which gives the pounds part (for "399" it becomes "3").

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

console.log(`£${pounds}.${pence}`);
//This takes the last 2 digits to get the pence part (for "399" it becomes "99") and ensures it is always 2 digits long by padding with zeros on the right if needed.

// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
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
// This prints the final formatted price by inserting pounds and pence into a template string, producing £3.99.

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// This program takes a string representing a price in pence
7 changes: 7 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

- It opens an alert saying "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`.

What effect does calling the `prompt` function have?

- It opens a prompt asking for an input

What is the return value of `prompt`?

- myName
13 changes: 13 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

- I am getting a function log() { [native code] }

Now enter just `console` in the Console, what output do you get back?

- console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`

- Object

Answer the following questions:

What does `console` store?

- It stores different data such as debug ,error ,info , log, warn, etc.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?

- 'console.log' :Accesses the log function on the console object so you can print output.
- 'console.assert' : Accesses the assert function on the console objects so you can report an error when the condition is false.
- '.': Dot notation is used to access a property or method on an object (e.g console.log gets log from console).
Loading