Skip to content
4 changes: 4 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

let count = 0;

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
In line 3 , the new item adds 1 to turn count into a new value .

1 adds into an old value and update its own value , it becomes a new value now.
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 = ``;

function first(firstName, middleName, lastName){

let initials = firstName.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1);

return `${initials}`;
}
console.log(first(firstName, middleName, lastName));


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

21 changes: 17 additions & 4 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");

// 1. Get the base (file.txt)
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 dir part of the filePath
const ext = filePath.slice(filePath.lastIndexOf(".") + 1);

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

const dir = ;
const ext = ;

Comment thread
LonMcGregor marked this conversation as resolved.
// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn









11 changes: 11 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,18 @@ const maximum = 100;

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

Math.floor is a function to make sure it is a whole number.

The fuction will randomly choose a decimal number from 0 - 1.
After that will do the Math(100-1+1) and multiply the decimal value and add 1.




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



5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
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?

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

const age = 33;
age = age + 1;
let age = 0; // Initialize the age variable
age = age + 1; // Reassign the value by 1
console.log(age);


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

because it doesn't have console.log, so it doesn't print the result.
but it gives the function errors.

it is not what I expected, because we should change the number into string, because it is not a
function.


// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const twelvehourClockTime = "8:53pm";
const twentyfourhourClockTime = "20:53";

console.log(twelvehourClockTime);
console.log(twentyfourhourClockTime);

30 changes: 28 additions & 2 deletions 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,37 @@ 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
6

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you read the task again? It asks you to write down the lines with function calls



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

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?
It lacks comma insides the replaceAll.

// c) Identify all the lines that are variable reassignment statements

// 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?
It is to take out the comma insides the number of carPrice.









20 changes: 14 additions & 6 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 3540; // length of movie in seconds

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

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

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);
const formattedtime = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(formattedtime);

// 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
// b) How many function calls are there?

1
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

It divides totalMinutes into 60, see how much remainingMinutes left.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
After deducting the totalMinutes, and divide the second. How much do he have the totalMinutes ?

// e) What do you think the variable result represents? Can you think of a better name for this variable?
We can use formmattedtime to point out the time has been formatted.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
for one hour = 3600 second . 1:00:00. No, it does not work here, because the remaining minutes and second forgets the extra zero.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//for 59 minutes = 3540 second . 0:59:0 No, it does not work here, because the remaining seconds and total hour forgets the extra zero.
for seconds = 6 seconds . 0:0:6. No, it does not work here, because the totalHours, remainingMinutes and remaining seconds forgets the extra zero.
30 changes: 29 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,32 @@ console.log(`£${pounds}.${pence}`);
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
1. const penceString = "399p": initialises a string variable with the value "399p"
2.
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
); the purpose of this is to take out the p.


3.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
if it is shorter than 3 characters, the 0 will add in front of the number ; but 399 is already 3 numbers so we donot need to do anything.



4. const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
); the length of the string is 3 , after -2 , it will become 1. The const pounds will take out 3 and it will become 3.


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

It will take out the number from the end to 1 ; so it will have 99.

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

Finally it will put the pounds and the pence into the template and print it out. The result will be £${3}.${99} ;
Loading