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

// Line 3 is updating the value of the count variable. The = operator is an assignment operator, which assigns the value on the right (count + 1) to the variable on the left (count). In this case, it takes the current value of count (which is 0), adds 1 to it, and then assigns the result (1) back to count. So after this line executes, count will have a new value of 1.
3 changes: 2 additions & 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,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];
console.log(initials);

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

6 changes: 4 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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 ext = filePath.slice(filePath.lastIndexOf("."));
console.log(`The ext part of ${filePath} is ${ext}`);

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


// minimum is 1, maximum is 100

// Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). So it can be any value from 0 up to but not including 1.
// (maximum - minimum + 1) calculates the range of possible values, which is 100 - 1 + 1 = 100. This means we want to generate a random number between 0 and 99 (inclusive) before adding the minimum value.
// Math.random() * (maximum - minimum + 1) gives us a random decimal number between 0 (inclusive) and 100 (exclusive). This means it can be any value from 0 up to but not including 100.
// Math.floor() takes the random decimal number and rounds it down to the nearest whole number. This means we will get an integer value between 0 and 99 (inclusive).
// Finally, we add the minimum value (1) to the result, which shifts the range from 0-99 to 1-100. So num will be a random integer between 1 and 100 (inclusive).
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/* const age = 33;
age = age + 1;
*/
// This code will throw an error because we are trying to reassign a value to a constant variable (age). In JavaScript, once a variable is declared with const, its value cannot be changed. To fix this error, we can either change the declaration to let or var, which allows for reassignment, or we can create a new variable to store the updated age. For example:

let age = 33;
age = age + 1;
console.log(age);
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// 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";
*/


// The error in this code is that we are trying to use the variable cityOfBirth before it has been declared and assigned a value. In JavaScript, variables declared with const (or let) are not hoisted, which means they cannot be accessed before their declaration. To fix this error, we need to declare and assign a value to cityOfBirth before using it in the console.log statement. For example:

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,9 +1,17 @@
const cardNumber = 4533787178994213;
/*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

// The code above won't work because cardNumber is a number, and the slice method is a string method. The slice method cannot be used on a number, which will result in a TypeError. To fix this error, we can convert cardNumber to a string before using the slice method. For example:


const cardNumber = 4533787178994213;
const last4Digits = cardNumber.toString().slice(-4);
console.log(last4Digits);
11 changes: 9 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
/*const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
*/

//
// The code above won't work because the variable names suggest that twelveHourClockTime should represent a time in 12-hour format, while twentyFourHourClockTime should represent a time in 24-hour format. However, the values assigned to these variables are not consistent with their names. To fix this error, we can either change the variable names to match the values or update the values to match the variable names. For example:

const twelveHourClockTime = "08:53 PM";
const twentyFourHourClockTime = "20:53";
15 changes: 14 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 @@ -12,11 +12,24 @@ 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
// There are 4 function calls in this file. The lines where a function call is made are:
// Line 1: carPrice.replaceAll(",", "")
// Line 2: priceAfterOneYear.replaceAll(",", "")
// Line 3: Number(carPrice.replaceAll(",", ""))
// Line 4: 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 occurring on 4 where we are trying to convert the string values of carPrice and priceAfterOneYear to numbers using the Number function. The error is occurring because the replaceAll method is being called on a string that has not been updated with the new value after the first replaceAll call. To fix this problem, we can update the carPrice and priceAfterOneYear variables with the new values after the replaceAll calls. For example:

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

// d) Identify all the lines that are variable declarations
// 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?
// The expression Number(carPrice.replaceAll(",","")) is first calling the replaceAll method on the carPrice string to remove all commas from the string. This is necessary because the presence of commas in a number string can cause issues when trying to convert it to a number. After the commas are removed, the resulting string is passed to the Number function, which converts the string into a numeric value. The purpose of this expression is to convert the carPrice string, which may contain commas, into a number that can be used for calculations.
7 changes: 6 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,19 @@ 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 in this program.

// b) How many function calls are there?
// There is 1 function call in this program, which is console.log(result).

// c) Using documentation, explain what the expression movieLength % 60 represents
// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. In this context, it is used to determine the number of seconds that are left after accounting for the full minutes in the movie length. Since there are 60 seconds in a minute, using the modulus operator (%) with 60 gives us the remaining seconds that do not make up a full minute. For example, if movieLength is 8784 seconds, then 8784 % 60 would give us the number of seconds remaining after dividing 8784 by 60, which is 24 seconds.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The expression assigned to totalMinutes is calculating the total number of minutes in the movie length. It does this by first subtracting the remaining seconds (calculated in the previous line) from the total movie length in seconds, which gives us the total number of seconds that are part of complete minutes. Then, it divides that result by 60 to convert the total seconds into minutes. For example, if movieLength is 8784 seconds and remainingSeconds is 24 seconds, then (8784 - 24) / 60 would give us the total number of minutes in the movie, which is 146 minutes.

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

// The variable result represents the formatted string that shows the length of the movie in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieDurationFormatted, as it more clearly indicates that it is a formatted representation of the movie length.
// 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 non-negative integer values of movieLength, as it is designed to convert a length in seconds into a format of hours, minutes, and seconds. However, if movieLength is a negative value, the calculations may not make sense in the context of a movie length, and the output may not be meaningful.
5 changes: 5 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,8 @@ 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): creates a new string variable that contains the original string without the last character (the "p"), resulting in "399"
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399"
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the substring representing the pounds by taking all characters except the last two, resulting in "3"
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the substring representing the pence by taking the last two characters and pads it with trailing zeros if necessary, resulting in "99"
// 6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence, resulting in "£3.99"
4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ Let's try an example.
In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;


What effect does calling the `alert` function have?
it will show a pop up with 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 will give a text box propmt with myName to enter my name.
What is the return value of `prompt`?
its a string.
8 changes: 8 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
function defination


Now enter just `console` in the Console, what output do you get back?
This prints the entire console object with all its methods, like log, warn, error, etc.

Try also entering `typeof console`
This prints the entire console object with all its methods, like log, warn, error, etc.

Answer the following questions:

What does `console` store?
object
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
console.log("Hello") calls the log method to print something to the console.

console.assert(condition, "Message") calls the assert method to check a condition, and only prints the message if the condition is false.