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

/*
it is making count be the sum of count + 1.
For example if this is part of a loop it will allow count to keep adding 1 each time the loop runs.
*/
2 changes: 1 addition & 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,6 @@ 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.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1);

// https://www.google.com/search?q=get+first+character+of+string+mdn
4 changes: 2 additions & 2 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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, filePath.lastIndexOf("/") + 1);
const ext = filePath.slice(-4);

// https://www.google.com/search?q=slice+mdn
8 changes: 8 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ 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
/*
Firstly its not running as it is showing as undefined as it is lacking something like console.log
After adding console.log(num) and running it a few times I get 62, 45, 35, 10.
For what it is doing: Math.floor is the sum of a random number from 0 to 0.9999... which is then multiplied by the maxium - minimun + 1, so 100 - 1 + 1.
That gives a number from 0-99, then then adds the minimun which is 1, so that gives a random number from 1-100.
*/
5 changes: 4 additions & 1 deletion Sprint-2/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?
We don't want the computer to run these 2 lines - how can we solve this problem?

// giving SyntaxError
// You need to add // to each line or /* before the first line and */ after the last one to make it a comment.
8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-errors/1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The error is not because it is being called again, it is because you are trying to reassign it to another value.

Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@

const age = 33;
age = age + 1;

// this gives a TypeError
// age is already a const at the start and calling it again just as age wont work, naming it like "older" would work better:

// const age = 33;
// let older = (age + 1);
// console.log(older)

5 changes: 5 additions & 0 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

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

//ReferenceError is happening as the const is after the console.log request for it, so the order is wrong.

// const cityOfBirth = "Bolton";
// console.log(`I was born in ${cityOfBirth}`);
17 changes: 15 additions & 2 deletions Sprint-2/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
// const cardNumber = 4533787178994213;
// const last4Digits = cardNumber.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

/*
cardNumber is giving a number and that can't be used for slice, could not remember the name for that error at this time.
after running the code it shows thats a TypeError. cardNumber.slice is not a function
*/

const cardNumber = 4533787178994213;
let number = cardNumber.toString()
const last4Digits = number.slice(-4);
console.log(last4Digits)



6 changes: 6 additions & 0 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";

// This is a Syntax error as cant have numbers for an identifier at the start of it.
// Changing to say: time12HourClock would work

// const time12HourClock = "8:53pm";
// const time24hourClock = "20:53";
11 changes: 10 additions & 1 deletion Sprint-2/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ 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
// 5 times its been called:
// 4 and 5 for Number and replaceAll
// 10 for 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?
// SyntaxError, its from line 5 in replaceAll("," "")
// its missing a , between the two arguments so it needs to be (",", "")

// c) Identify all the lines that are variable reassignment statements
// There are two reassignment statements in lines 4 and 5

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

// They are 1,2 and 7,8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// It is using Number to make the string into a number but in order to do that the string needs to contain numerical letters only.
// So it then uses in the () to remove all of the , with nothing using the replaceAll function so that 10,000 becomes 10000
21 changes: 14 additions & 7 deletions Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
const movieLength = 8784; // length of movie in seconds
// const movieLength = 8784; // length of movie in seconds

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

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

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);
// const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
// 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?
// There are 6 variable declarations, lines 1,3,4,6,7,9

// b) How many function calls are there?
//1 at line 10

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
//This is using a remainder, this us seeing how many times 8784 goes into 60 and what is remaining which is 24 in this case:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// It is the sum of movieLength minus remainingSeconds then divided by 60

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// its showing the length of the movie in hours, minutes and seconds, could be better called something like MovieDuration

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//Yes it will as long as the input is numerical numbers only, it does not work with any extra symbols, spaces or letters.
21 changes: 21 additions & 0 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
/*
3-6. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1)
This is taking penceString and removing the p from it so its just a string with the numerical letters only.

8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
This is taking penceStringWithoutTrailingP ensuring that the minimum length of the numerical string is at least 3 numbers.
It is filling out with 0s from the front so if it was 3 it would have 003

9-12. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
this is taking paddedPenceNumberString and removing the last 2 numerical numbers from the string/
This takes the 399 and makes it 3


14-16. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
This is taking paddedPenceNumberString and first reduces the length to the last 2 numbers in the string.
It then uses padEnd to ensure that there is a at least two 00 if no numbers are present.

18. console.log(`£${pounds}.${pence}`);
This shows the pounds and pence with a £ at the start and a . between them.
making this show the pence to pounds as £3.99
*/
Loading