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
6 changes: 6 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,9 @@ 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 try to increment the variable count by 1.
* after the = operator we use the current count variable (which is 0) and add 1 to it.
* then we reassign the result back to the count variable.
**/
5 changes: 3 additions & 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,8 @@ 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[0]}${middleName[0]}${lastName[0]}`;

// https://www.google.com/search?q=get+first+character+of+string+mdn
console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn
9 changes: 6 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ 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);
console.log(`The dir part of ${filePath} is ${dir}`);
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex);
console.log(`The ext part of ${filePath} is ${ext}`);

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

/**
* The expression is generating a random integer between the minimum and maximum values.
* Math.floor() will round down the number to the nearest integer.
* Math.random() will generate a random number between 0 and 1 (not including 1).
* The expression (maximum - minimum + 1) calculates the range of possible values.
* Multiplying Math.random() by this range scales the random number to the desired range.
* Adding minimum shifts the range to start from the minimum value.
* Finally, Math.floor() ensures that the result is an integer.
**/
console.log(num);
//example output: 42
//example output: 7
//example output: 56
//example output: 1
//example output: 48
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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 error occur because these are plain text not code
//we can turn them into comment by adding // at the beginning of each line
//or we can put them inside /* and */ to turn them into block comment
5 changes: 5 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,8 @@

const age = 33;
age = age + 1;

// this error occur because we are trying to reassign a value to a constant variable
// to solve this problem we can change the const to let
// let age = 33;
// age = age + 1;
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";

// this error occur because we are trying to use the variable cityOfBirth before it is declared
// to solve this problem we can move the declaration of cityOfBirth before the console.log line
// const cityOfBirth = "Bolton";
// console.log(`I was born in ${cityOfBirth}`);
6 changes: 6 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,9 @@ 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

// This error occur because we are trying to use the method slice on a number
// To solve this problem we can convert the number to a string by using toString() method
// const last4Digits = cardNumber.toString().slice(-4);
// console.log(last4Digits);
// example output: 4213
7 changes: 6 additions & 1 deletion 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 24hourClockTime = "08:53";

// This error occur because variable names cannot start with a number
// To solve this problem we can rename the variables to start with a letter
// const hour12ClockTime = "20:53";
// const hour24ClockTime = "08:53";
29 changes: 29 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,40 @@ 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
/*
4 functions calls in line 4 and line 5
Line 4: carPrice.replaceAll(",", "")
Line 4: Number(carPrice.replaceAll(",", ""))
Line 5: priceAfterOneYear.replaceAll("," "")
Line 5: Number(priceAfterOneYear.replaceAll("," ""))
*/

// 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 coming from line 5
this error occur because it miss a comma in the expression priceAfterOneYear.replaceAll("," "")
to solve this problem we can add a comma between the two quotation marks
priceAfterOneYear.replaceAll(",", "")
*/

// c) Identify all the lines that are variable reassignment statements
/*
2 variable reassignment statements in line 4 and line 5
Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/

// d) Identify all the lines that are variable declarations
/*
4 variable declarations in line 1, 2, 7 and 8
Line 1: let carPrice = "10,000";
Line 2: let priceAfterOneYear = "8,543";
Line 7: const priceDifference = carPrice - priceAfterOneYear;
Line 8: const percentageChange = (priceDifference / carPrice) * 100;
*/

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*
this expression is converting the string carPrice to a number
it will return NaN if the string cannot be converted to a number
*/
35 changes: 34 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,47 @@ 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 variable declarations in line 1, 3, 4, 6, 7 and 9
Line 1: const movieLength = 8784; // length of movie in seconds
Line 3: const remainingSeconds = movieLength % 60;
Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60;
Line 6: const remainingMinutes = totalMinutes % 60;
Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60;
Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
*/

// b) How many function calls are there?
/*
1 function calls console.log() in 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
/*
The expression movieLength % 60 is using the modulus operator (%).
This operator returns the remainder of a division operation.
In this case, it divides movieLength (which is 8784) by 60.
MovieLength % 60 gives us the remaining seconds after divided by 60.
*/

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/*
The expression (movieLength - remainingSeconds) / 60 is calculating the total number of whole minutes in the movie length.
First, it subtracts the remaining seconds (calculated in line 3) from the total movie length in seconds (movieLength).
This gives the total number of seconds that can be fully converted into minutes.
Then, it divides that result by 60 (the number of seconds in a minute) to convert those seconds into minutes.
The final result is assigned to the variable totalMinutes, which represents the total number of whole minutes in the movie length.
*/

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

/*
The variable result represents the duration of the movie.
A better name for this variable could be movieDuration.
*/
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
/*
This code will work for all positive integer values of movieLength.
It will correctly calculate the hours, minutes, and seconds for any length of movie given in seconds.
However, if movieLength is a negative number or a non-integer (like a decimal), the code may not produce meaningful results.
*/
8 changes: 8 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

/*
2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): extract the whole string except the last character from the penceString variable and assign to a new variable penceStringWithoutTrailingP
3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): ensures that the string has at least 3 characters by adding with leading zeros if necessary
4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the whole string except the last two characters
5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the last 2 characters and ensure it has two digits by adding with zeros after the remaining character if necessary
6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence to the console
*/
3 changes: 3 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
it call a pop up with your message and an 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?
It will call a pop dialog with a message and an input field. It has a ok and cancel button as well.
What is the return value of `prompt`?
It returns the value of what was wrote on the input field.
8 changes: 5 additions & 3 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter
What output do you get?

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

ƒ log() { [native code] } it's a function
Try also entering `typeof console`

'object'
Answer the following questions:

What does `console` store?
What does `console` store? console store an object that contain some functions.
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
the syntax 'console.log' or 'console.assert' means u re trying to access a function of the console object.
the dot is used to access the function of an object
18 changes: 17 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

/*
My prediction is that this code will produce an error because there is a variable name conflict.
*/
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -10,4 +12,18 @@ function capitalise(str) {
}

// =============> write your explanation here
/*
The error is occurring because there is a variable name conflict.
Inside the function capitalise, there is a parameter named str and a variable declared with let also named str.
This causes a syntax error because we cannot declare two variables with the same name in the same scope.
To fix this problem, we can rename the variable declared with let to a different name.
*/

// =============> write your new code here

function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}

console.log(capitalise("hello")); // example output: "Hello"
20 changes: 19 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
/*
My prediction is that this code will produce an error because there is a variable name conflict.
and the console.log statement is trying to access decimalNumber which is not defined in that scope.
*/

// Try playing computer with the example to work out what is going on

Expand All @@ -15,6 +19,20 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here

/*
The error is occurring because there is a variable name conflict.
Inside the function convertToPercentage, there is a parameter named decimalNumber and a variable declared with const also named decimalNumber.
This causes a syntax error because we cannot declare two variables with the same name in the same scope.
To fix this problem, we can rename the variable declared with const to a different name.
and also, the console.log statement is trying to access decimalNumber which is not defined in that scope.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here

function convertToPercentage(decimalNumber) {
const percentageValue = decimalNumber;
const percentage = `${percentageValue * 100}%`;

return percentage;
}
console.log(convertToPercentage(0.75));
18 changes: 16 additions & 2 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@

// Predict and explain first BEFORE you run any code...


// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
/*
My prediction is that this code will produce an error because the parameter name is not a valid identifier.
*/

function square(3) {
return num * num;
}

// =============> write the error message here

/*
SyntaxError: Unexpected number
*/
// =============> explain this error message here

/*
The error is occurring because the parameter name "3" is not a valid identifier in JavaScript.
In JavaScript, parameter names must start with a letter, underscore (_), or dollar sign ($) and cannot be a number.
To fix this problem, we can rename the parameter to a valid identifier, such as "num".
*/
// Finally, correct the code to fix the problem

// =============> write your new code here

function square(num) {
return num * num;
}

console.log(square(4));
17 changes: 15 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// Predict and explain first...

// =============> write your prediction here

/*
My prediction is that this code will produce an error because the function multiply does not return any value.
*/
function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here

/*
The error is occurring because the function multiply does not return any value.
When we call multiply(10, 32), it calculates the product but it does not return the result.
Therefore, when we try to use the return value of multiply in the console, it results in undefined.
To fix this problem, we need to add a return statement in the multiply function to return the product of a and b.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here

function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // example output: The result of multiplying 10 and 32 is 320
14 changes: 14 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...
// =============> write your prediction here
/*
My prediction is that this code will produce an error because the function sum does not return any value.
*/

function sum(a, b) {
return;
Expand All @@ -9,5 +12,16 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
/*
The error is occurring because the function sum does not return the expected sum of a and b.
The return statement is currently returning undefined because there is no expression after the return keyword.
To fix this problem, we need to modify the return statement to return the result of a + b.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here

function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
Loading