Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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.
Comment thread
cjyuan marked this conversation as resolved.
Outdated
10 changes: 9 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,15 @@ 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 letter1 = firstName.charAt(0);
let letter2 = middleName.charAt(0);
let letter3 = lastName.charAt(0);

let initials = letter1 + letter2 + letter3;
console.log(initials);



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


7 changes: 5 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,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 ext = filePath.slice(lastSlashIndex + 1).split('.').pop();
console.log(`The ext part of ${filePath} is ${ext}`);

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


//We start with 2 constant (unchangeable) values
//We use them to generate a random number between 1 and 100 with Math.random()
//The random number will always be an integer because Math.floor() is used to round
//down the result.
//At the end the value "minimum" is added to guarantee that the generated "num" is at least 1
//"num" is a random number generator with results between 1 and 100.
Comment thread
cjyuan marked this conversation as resolved.
Outdated
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
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?


//Starting a line with two forward slashes creates a comment.
//This makes the computer ignore the rest of that line; it will not attempt to execute it.
//It is advisable to use this feature lots because explaining the intention of the code
//is helpful when cooperating with other people (and for your future self)
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;

// The reassignment will not work because the variable is a "const" = a constant,
// which means it cannot be changed.
// If a value will need to be changed later it must be declared with "let"
// instead of "const".
6 changes: 6 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,9 @@

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


// These two lines are in the wrong order.
// The request is to print information that has not yet been received.
// In order for this to work both the declaration of the variable and its value
// need to be declared before the console.log line
11 changes: 11 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,14 @@ 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 card number is not declared as a fixed unit = a string, it is just a set of numbers.
// Also, the slice method is only suitable for strings.

// First the card number needs to become a string, Then it can be sliced.

const last4Digits = String(cardNumber).slice(-4);
console.log(`The last 4 digits of the card number are ${last4Digits}`);


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

// A variable name cannot start with a number. It should rather be e.g. twelveHourClockTime.
// There's a mistake in the assignment of the clock times: the 12h clock has a 24h time
// I'm not sure how well it works for these times to be declared as unchangeable "const"?
// Not wrong wrong but bad practice is that the second variable has a lowercase h for Hour
25 changes: 25 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,35 @@ 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 4 function calls in this file:

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

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Comment thread
cjyuan marked this conversation as resolved.


// 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 is a comma missing in: replaceAll("," ""));
// It should be: replaceAll(",", ""));

Comment thread
cjyuan marked this conversation as resolved.
Outdated

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

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?

// The purpose is to remove the comma from all amounts, so that they have the correct format to be seen as numbers
// instead of as strings. Then Number is used to do this conversion from string to number.

28 changes: 28 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,41 @@ console.log(result);

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

// There are 6: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours,
// and result


// b) How many function calls are there?

// There are 2: console.log and the template literal for the result string
Comment thread
cjyuan marked this conversation as resolved.
Outdated


// 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 calculates the remainder when movieLength is divided by 60.
// For instance if the movieLength were 121, the remainder of this division would be 1.



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

const totalMinutes = (movieLength - remainingSeconds) / 60;

// This line calculates the length of the movie in whole minutes


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

// "result" represents the result of the calculations, being the total length of the movie
// transcribed to hours/minutes/seconds. It would have been much clearer to not give the mathematical
// output as the name but instead the usability of this output, such as "movieLengthInHours"
Comment thread
cjyuan marked this conversation as resolved.
Outdated



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

// Yes, as long as they are positive numbers. They should also be integers.
// I have tried to give half-seconds and this did not break the code, but the half-second was
// ignored in the output

14 changes: 14 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,17 @@ console.log(`£${pounds}.${pence}`);

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

// 2. The next step in lines 3-6 strips the letter p from the amount.
// Without the p the amount-string becomes a number: 399

// 3. In lines 8-12 the number is padded with leading zeros if needed to ensure it has at
// least three digits. This is needed for the last step: the conversion into an amount in
// pounds, where the format requires at least three digits

// 4. In lines 14-17 the number is split into pounds and pence.
// In line 16 the number is padded with trailing zeros to ensure the pence always have two digits
Comment thread
cjyuan marked this conversation as resolved.

// 5. Then in line 20 the pounds and pence are put in a string with the pound sign in front of
// it, and printed to the console