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: 4 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ let count = 0;

count = count + 1;

// The assignment operator (=) stores the result of count + 1 in the variable count. i.e. incrementing the count by 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


13 changes: 9 additions & 4 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// 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 firstName = "Creola";
let middleName = "Katherine";
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 = firstName[0] + middleName[0] + lastName[0];

console.log(initials);

//I have run this code on chrome console and the answer was CKJ


let initials = ``;

// 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 @@ -15,9 +15,10 @@ 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
const dir = filePath.slice(0, lastSlashIndex);

// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;
const ext = base.slice(base.lastIndexOf(".") + 1);

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

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


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

//Expressions in JS are evaluated from inside out, following the brackets. So (maximum - minimum + 1) would become 100-1+1=100.
//Then we move on to the next outer bracket "Math.random()", which is a function designed to generate random numbers. We multiply that random number
//by 100 and that is our brackets calculated for now. Next in the expression is the function "math.floor" which rounds down numbers to the
//nearest whole number, to which we add the minimum at the end. Which is number 1.

// I have run this code on my Chrome console multiple times and got a diffrent number each time.

const minimum = 1;
const maximum = 100;

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

console.log(num);
10 changes: 9 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
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?

//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?
*/
11 changes: 11 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,14 @@

const age = 33;
age = age + 1;

/*
Error here is that value for "const" cannot be reassigned. If we wanna see the code work
we must use "let" keyword because the variable can be changed.
*/

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

//I have ran this code and got 34 on chrome console
5 changes: 5 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,8 @@

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

//We must declare the variable first then use it, because JS reads from top to bottom.

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



/*
The code won't work because the cardNumber is a numeric value and "slice()" works with strings in JS.
I have run the code to get "VM1691:2 Uncaught TypeError: cardNumber.slice is not a function" error.
I guess, JS is trying to find slice function on the cardNumber and since it is not a string it confirms
that it is in fact not a function.
*/

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

console.log(last4Digits);

//this code worked because i have converted the cardNumber value to string then sliced the last four digits.
7 changes: 7 additions & 0 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";

/*
I have predicted the code won't run due to the fact that "8:53pm" should be "08:53"
Got this error "Uncaught SyntaxError: Invalid or unexpected token", meaning it is a syntax error.
I have searched it up to find that variables can't start with numbers because it makes it easier
for JS to read code by removing a mathematical ambiguity.
*/
21 changes: 21 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,31 @@ 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 call names, but two functions have been made twice inside another, adding up to five function calls being made overall.
1. carPrice.replaceAll(",", "")
2. Number(carPrice.replaceAll(",", "")) this expression contain two, one inside the other
3. priceAfterOneYear.replaceAll(",", "")
4. Number(priceAfterOneYear.replaceAll(",", "")) this expression contains two as well
5. 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?

//I have run the code and got a syntax error on line 5, due to two arguments not being seperated by a comma.

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

//Removing all the comma for calculation then convert the string into 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?

// Six, every line that starts with "const" keyword.

// b) How many function calls are there?

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

// It calculates how many seconds are left over.

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

// It first removes the leftover seconds from the total movie length, then converts the remaining seconds into minutes.

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

// It represents a formatted time string and since it's a movie, i would change "result" variable to "movieDuration".

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

// The code wouldn't work with decimal numbers "because movieLength" is a whole number, also negative numbers won't work
//because movies can't be less than zero.
35 changes: 24 additions & 11 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
const penceString = "399p";
const penceString = "399p"; // This line shows price in pence with a letter "p" attached at the end.

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

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

// Above line of code removes the "p", using "penceString.substring" function by removing the last character with "-1",
//out of a four digit string "399p".

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
// We are using "padstart" method to ensure the string contains three characters at least,
//and if necessary to add "0" to the front.


const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);

// We are calculating the pounds const variable by taking away the last two digits from "paddedPenceNumberString" const.

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

//First we extracting the last two digits with "(paddedPenceNumberString.length - 2)", then making ensure the pence part
//has two digits always with "padEnd(2, "0")". Basically calculating the const pence variable.

console.log(`£${pounds}.${pence}`);

//Uses a template literal to combine the pounds and pence values into a currency format and prints the result to the console.





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

Expand Down
Loading