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

let count = 0;

count = count + 1;
Expand Down
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

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 @@ -11,13 +11,15 @@

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}`);

const dir = filePath.slice(0, lastSlashIndex);

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

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

const age = 33;
age = age + 1;
function X(num){
const age = num +1;
return age;}
console.log(X(33));
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);

26 changes: 24 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,33 @@ 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 here. There are two when you use Number();
The other call is about console.log() to print the string out.
When you call the variable (priceDifference/ carPrice), it is a function call.
The last one is

// 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 shows that I miss a ) parenthesis.


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

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

// e) Describe what is the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

It deletes all the comma insides.I
The Number helps convert that string into an actual figure.



17 changes: 17 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@ 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?
Six, the const with the functions inside.


// b) How many function calls are there?
4

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

It is called the remaining function, because if you use this function , you will use the remainingMinutes to divides 60 and get the remaining value .


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
It calls the variable movieLength and remainingSeconds and substract together and divide it 60.



// e) What do you think the variable result represents? Can you think of a better name for this variable?
I think it represents the total hour , remaining minutes and the remaining seconds .Duration is a better name



// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
yes
I tried to put 6(second), 60(1 minute), 3600(equivalent to 1 hour)



15 changes: 14 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,17 @@ 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"

line 1. const penceString = "399p": initialises a string variable with the value "399p"
line 3. You define a penceStringWithoutTrailingP variable and call a function to cut a line, you use penceString.length -1 because
you want to cut the p.

line 8: You define a variable paddedPenceNumberString and call a function to add 0 into the letters, if the words are not three characters long.
line 9: You want to take the pound out of 399, so you call the function substring and only wants before the index 1, which is calculated by (0 , length -2);

line 14: You call a variable function and you subtract the value from index 1 to the end.
line 18: print the variable pound and the variable pence with the .




26 changes: 19 additions & 7 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
// =============> write your new code here
It said syntaxError because identifier 'str' has already been declared
because we should use backtick`` to wrap the template literal.The first one of the bracket is a
single quote, not a backtick.

Also, since str has already been declared as a parameter, we should not use it to define as variable.





function capitalise(str) {

let result = `${str[0].toUpperCase() + str.slice(1)}`;
return result;
}

console.log(capitalise("str"));

25 changes: 15 additions & 10 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -14,7 +7,19 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
The terminal shows the syntax error because decimal number has been declared.
because the decimal number is a variable which is not supposed to define in the function,
but the decimal number has been defined insides the functon, so it will forever return 50 %.
Also, when we call the fucnton, we should name the function convertToPercentage.



function convertToPercentage(decimalNumber) {

const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.5));

// Finally, correct the code to fix the problem
// =============> write your new code here
18 changes: 10 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

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

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

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

// =============> explain this error message here
It shows the syntax error which is an unexpected number;
because we should not put 3 insides the variable, because it will lead to variable: num not defined
insides the function, so the 3 doesn't work insides the function.

We need to put num instead of 3 insides the();
That way when we call the function by using console.log.The 3 will go into the num variable.


// Finally, correct the code to fix the problem
function square(num) {
return num * num;
}

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


16 changes: 11 additions & 5 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// Predict and explain first...

// =============> write your prediction here
The problem is unexpected identifier "problem"
The problem is the result doesn't print the mulitplication of the number,
because we should not call the function inside the function;


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
Instead, we should define the expression a * b
and return the expression.

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

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
18 changes: 13 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
Unexpected identifier "problem"
variable hasn't been defined so the variable cannot do function.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,14 @@ function sum(a, b) {

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

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

because return should be followed by a + b;
the final result doesn't return the value.


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

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