Skip to content
Open
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ 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

//line 3 is updating the value of the variable count to 'count+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.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;
console.log(initials);

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

13 changes: 9 additions & 4 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
// (All spaces in the "" line should be ignored. They are purely for formatting.)

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
const lastSlashIndex = filePath.lastIndexOf("/");// result gives the index value until the last '/'
const base = filePath.slice(lastSlashIndex + 1); // result will slice the 'filePath value with the number output from lastIndexOf
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) ;
const lastDotIndex=base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);

console.log(dir);
console.log(ext);

// https://www.google.com/search?q=slice+mdn
9 changes: 8 additions & 1 deletion Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ const minimum = 1;
const maximum = 100;

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

console.log(num);
// 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

//(maximum - minimum + 1) this will give a result of 99+1
//Math.random() will give a random number lesser 1
//Math.floor(Math.random() * (maximum - minimum + 1)) this expression will remove the decimal point and round down the value
//+ minimum this is the last part to be evaluated, it will add 1 to the first part

//Answer: num will give a random value from 0 to 100
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?*/

//I have commented the 2 lines since it is not meant for the computer to run
2 changes: 2 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,5 @@

const age = 33;
age = age + 1;

// Age was assigned as a constant variable, it can't be reassigned. We can change it to let, let age=33;
2 changes: 2 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,5 @@

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

//Because the cityOfBirth was not defined yet before printing it, there's nothing to print
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ const last4Digits = cardNumber.slice(-4);
// 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?
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?-It gives an error because .slice is a string method and not for a number.




// Then try updating the expression last4Digits is assigned to, in order to get the correct value

// const cardNumber = 4533787178994213;
// const last4Digits = cardNumber.slice(-4);
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 can't start with a number

// const twelveHourClockTime = "20:53";
// const twentyFourHourClockTime = "08:53";
23 changes: 23 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,33 @@ 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 5 function calls,
line 4 carPrice.replaceAll()
line 4 Number()
line 5 priceAfterOneYear.replaceAll()
line 5 Number()
line 10 console.log()
*/

// 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's a syntax error in line 5, there's no comma in between arguments
it should be priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/


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

/*There are 2 variable reassignments
line 4, carPrice = Number(carPrice.replaceAll(",", ""));
line 5, priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// d) Identify all the lines that are variable declarations

/*Variable Declarations are lines 1,2,7 and 8

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

//It removes all the commas converting the string to a number so it can be used in the math operations

19 changes: 16 additions & 3 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,26 @@ console.log(result);

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

// b) How many function calls are there?
/*
There are 6 variable declarations
const movieLength
const remainingSeconds
const totalMinutes
const remainingMinutes
const totalHours
const result
*/

// b) How many function calls are there?-There's only 1 function call

// 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 represents modulo operator, using %60 helps you extract how many seconds remaining.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
//totalMinutes represents how many complete minutes fit inside the movie length.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// e) What do you think the variable result represents? Can you think of a better name for this variable?-the result variable formats the movie time to a string showing hours, minutes and seconds, it can be renamed to formattedTime

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer-Yes as long as the movieLength value is positive
46 changes: 41 additions & 5 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
0,
0,
penceString.length - 1
);
);// will remove 'p' so the answer is 399

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");//this won't do much for the given since it's already 3 numbers
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
);// this code will remove the first digit,

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
.padEnd(2, "0");//this code will add '0' in front of the pence if it has 1 number

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

Expand All @@ -25,3 +25,39 @@ 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
);
:remove the trailing 'p' from the string.substring(0, penceString.length - 1) takes characters from index 0 up to (but not including) the last character.
*/


/*
3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
:Ensure the string has at least 3 characters by adding leading zeros if necessary.
*/



/*
4. const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
: This will extract the first digit, the pounds portion
*/

/*
5.const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");//this code will add '0' in front of the pence if it has 1 number
: this code extracts the pence portion, makes sure the it has exactly 2 digits
*/

/*
6. console.log(`£${pounds}.${pence}`);
:prints the new formatted value of pounds and pence
*/
6 changes: 5 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
//Function capitalise will make thes tring input first letter to Big case

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +10,8 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your explanation here : str has been declared in the function parameter and inside the function
// =============> write your new code here
function capitalise(str) {
return str `${str[0].toUpperCase()}${str.slice(1)}`;
}
8 changes: 7 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...
//the code was supposed to convert a number input into percentage

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here: An error will occur because "decimalNumber" has been redeclared inside the function

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

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

// =============> write your explanation here
//a variable can only be declared once, and const value cannot be replaced

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}
console.log(decimalNumber);
12 changes: 7 additions & 5 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 will not run because variables num are not declared
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> write your prediction of the error here: num is not defined

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

// =============> write the error message here
// =============> write the error message here: " Illegal return statement"

// =============> explain this error message here
// =============> explain this error message here: The return is outside the function, Javascript is treating is at function since the parameter is a value and not an identifier that's why the return inside was not treated as a function

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
9 changes: 7 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// =============> write your prediction here: the code block will display "The result of multiplying 10 and 32 is 320 "

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
// =============> write your explanation here: my prediction is wrong as the first console.log displays nothing as it doesn't have values yet and console.log doesn't hold a value it only displays

// 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)}`);
10 changes: 8 additions & 2 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
// =============> write your prediction here: this will result to an error because return doesn't have anything to perform

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

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

// =============> write your explanation here
// =============> write your explanation here: Operation a+b is after the return code, after return the function ends immediately

// 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)}`);
12 changes: 10 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here: it will always display that the last number of all input is 3

const num = 103;

Expand All @@ -16,9 +16,17 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// =============> write your explanation here :num is declared as const, so num will always be 103 and last digit will always be 3
// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(num) {
return num.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
5 changes: 3 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height); // divide weight by height squared
return bmi.toFixed(1); // round to 1 decimal place
}
3 changes: 3 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
function toUpperSnakeCase(str) {
return str.toUpperCase().replace(/ /g, '_'); //.replace(/ /g, '_') → replaces all spaces with underscores (_).
}
Loading