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
3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let count = 0;

count = count + 1;
count = count + 1;
// line 3 is assigning the variable "count" a new value of "count +1", then storing it back into "count".

// 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
2 changes: 1 addition & 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,7 @@ 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

7 changes: 4 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);

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 = filePath.slice(lastDotIndex+1);

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

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
//num can be any generated random number between 1 and 100, rounded down.





// 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
Expand Down
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?

// I have erased the above two lines, to prevent the computer from running them.
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;
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// 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}`);
//console.log(`I was born in ${cityOfBirth}`);
//const cityOfBirth = "Bolton";

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

const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = String(cardNumber).slice(-4);

//My prediction was the long card number isn't a string so js will assume it's a variable
//Error given: cardNumber.slice is not a function
//added String function


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

const twentyFourHourClockTime = "20:53";
const twelveHourClockTime = "08:53";

//variable names in js cannot start with numbers
21 changes: 19 additions & 2 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -13,10 +13,27 @@ 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 three function calls: replaceAll() - used twice, number() - used twice, console.log() - used once.
// carPrice = Number(carPrice.replaceAll("," , ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , ""));
// console.log(`The percentage change is ${percentageChange}`);

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

//There was a syntax error on line five, missing comma. Fixed by adding one.

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

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

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

//let carPrice = "10,000";
//let priceAfterOneYear = "8,543";
//const priceDifference = carPrice - priceAfterOneYear;
//const percentageChange = (priceDifference / carPrice) * 100;

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

// It's removing the commas, since javascript will recognise 10,000 as a string not a number.
13 changes: 13 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ console.log(result);

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

// There are six, all six lines of code use const as a variable declaration.

// b) How many function calls are there?

//There is only one funsction call, console.log(result)

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

// It represents how many seconds remain, before converting them to minutes first.

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

// it represents the movie length converted to minutes, without the remaining seconds.

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

//It represents the film length in a time string format. Better alternative could be formattedDuration, as it is self-explanatory,
//as opposed to generic 'result'.

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

// I have tried values between 0-9, ie all positive/whole numbers, and it works. However there is no input validation, ie negative numbersm, non-integers etc, so it won't work properly there.
14 changes: 7 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,27 @@
const penceString = "399p";

// 1. const penceString = "399p": initialises a string variable with the value "399p"
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

//2. Removes the trailing "p" from the pencestring, leaving 399 as the new value.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//3. This line ensures there are three characters, at least, by adding "0" to the start if necessary.
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

//4. We are extracting the pounds now, ie the first character.
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

//4. Here, we are extracting the last two digits, ie pence. Padend insures there are two digits and adds a "0" to compensate, if necessary.
console.log(`£${pounds}.${pence}`);
//5. Here it should show the final result of £3.99


// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds

// 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"
9 changes: 9 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,16 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

1. This function displays a pop-up box with Hello world written on it, with on OK button.

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?

2. It displays a pop-up box with What's your name written on it and an empty dialog box inside, awaiting my input.
Also, there are two options of OK and Cancel.

What is the return value of `prompt`?

3. If I enter my name undefined appears, same if i press the cancel button. Fixed this by adding myName at the end.
Now the value when i enter my name shows "my name", if i press cancel it shows "null".
17 changes: 17 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,28 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

1. I got back, 'ƒ log() { [native code] }'
which is the definition of console function.

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

2. I got back, console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
Which is the definition of console as an object with several examples of functions after the object.

Try also entering `typeof console`

3. I've got 'object', it is showing me that console itself is an object.

Answer the following questions:

What does `console` store?

4. The object console stores different functions used for various purposes, such as debugging, showing errors etc.

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

5. console.log allows you access the log property of the console object. ie log is a property stored inside the object. In this case log as a function.

console.assert - assert is another function stored inside console object, used for checking is a condition is true.

"." means go inside this property and get the property provided. Perfect example of this is, "person" is an object, "name" is a property and so "." allows me to access the property.
Loading