Skip to content
Closed
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
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 = ``;
const initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
console.log( `acronym = ${initials}`);

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

16 changes: 13 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
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
// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;
const lastDot = filePath.lastIndexOf(".");
console.log(`The indexDot is ${lastDot}`);

const ext = filePath.slice(lastDot);

const fileName = filePath.slice(lastSlashIndex + 1, lastDot);
const dir = filePath.slice(1, lastSlashIndex + 1);



console.log(`The dir is -> ${dir}`);
console.log(`The nameFile is -> ${fileName}`);
console.log(`The extension is -> ${ext}`);

// https://www.google.com/search?q=slice+mdn
4 changes: 4 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,7 @@ 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
Math.floor() //removes decimal part and returns whole number
Math.random() //needs to values between minimum and maximum to generate a random number
console.log(maximum - minimum + 1 ) + minimum; // this is same as 100 - 1 + 1 = 100
console.log(`The random number is ${num}`);
3 changes: 2 additions & 1 deletion 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;
let age = 33; // I to used 'let' to reassign a variable
age = age + 1;
console.log(age);
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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";
console.log(`I was born in ${cityOfBirth}`);
// The variable cityOfBirth has to be before the console.log statement.
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const numberString = cardNumber.toString(); // We have to convert the cardNumber to a string first before slicing the last 4 digits.
const last4Digits = numberString.slice(-4);
const num = Number(last4Digits);

console.log(num);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
9 changes: 7 additions & 2 deletions 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 T12HourClockTime = "20:53";
const T24hourClockTime = "08:53";
console.log (T12HourClockTime);
console.log (T24hourClockTime);
/*const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
JS variable names (identifiers) must not start with a digit only start with a letter*/
16 changes: 9 additions & 7 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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

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

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 3 function (lines 4,5, and 10)
// 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?

// line 5, the replaceAll method is missing a comma between its arguments.
// c) Identify all the lines that are variable reassignment statements

// 4 and 5)
// d) Identify all the lines that are variable declarations

// 1, 2, 7, and 8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*The expression Number(carPrice.replaceAll(",", "")) first removes the commas from the string value of carPrice.
After that, it converts the cleaned string into a number.
The purpose is to make sure the value can be used in mathematical calculations.*/
10 changes: 6 additions & 4 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,16 @@ 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
// b) How many function calls are there?

// There is 1 function call in the program: 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.
// 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?

// This line removes the leftover seconds from the total movie length and then divides the result by 60 to convert the remaining time into whole minutes.
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// The variable result stores the movie length formatted
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// The code will work correctly for positive whole numbers representing seconds.
9 changes: 6 additions & 3 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
const penceString = "399p";

// initialises a string variable with the value "399p"
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// Removes the final character ("p") from the string, leaving only the numeric part of the price.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// Ensures the string has at least three characters by adding leading zeros if necessary.
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
//Takes the last two digits as the pence part and ensures it is exactly two characters long.


console.log(`£${pounds}.${pence}`);
// Combines the pounds and pence into a formatted currency string and outputs the final result in pounds (£)

// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
Expand Down
14 changes: 8 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Predict and explain first...
// =============> write your prediction here
// =============> When we call capitalise("hello"), it will throw an error instead of returning "Hello".

// 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)}`;
return str;
// =============> "str" has already been declared, in JavaScript we can not redeclare a parameter using let inside the same scope.
// =============>

function capitalise(str) {

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello"));
19 changes: 9 additions & 10 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> An error will occur because console.log(decimalNumber) tries to access a variable that does not exist in the global scope.

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// =============> decimalNumber is already a parameter, and JavaScript does not allow redeclaring a parameter woth const.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

console.log(convertToPercentage(0.5));
15 changes: 8 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> SyntaxError

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

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

// =============> explain this error message here
// =============> function parameters must be variable not values.

// Finally, correct the code to fix the problem

// =============> write your new code here
// =============>
function square(num) {
return num * num;
}
console.log(square(3));


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

// =============> write your prediction here
// =============> SyntaxError undefined 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
// =============> We use console.log() only when we want to print something immediately. In this case we need to use return.

// 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)}`);
15 changes: 7 additions & 8 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Predict and explain first...
// =============> write your prediction here
// =============> the sum is undefined

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

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

// =============> write your explanation here
// =============> This is due to Automatic semicolon Insertion
// 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 + 32 is ${sum(10, 32)}`);
25 changes: 16 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> The output will always be 3 for all cases.


// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is 3
//The last digit of 105 is 3
//The last digit of 806 is 3
// Explain why the output is the way it is
// =============> write your explanation here
//The function does not have a parameter.
//It does not use the values 42, 105, or 806.
//It always uses the global variable:
// Finally, correct the code to fix the problem
// =============> write your new code here
const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
function getLastDigit(value) {
return value.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
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
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
7 changes: 5 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,8 @@
// 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 squareHeight = height * height;
const bmi = weight / squareHeight;
return Number(bmi.toFixed(1));
}
console.log(calculateBMI(60, 1.70));
6 changes: 6 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,9 @@
// 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().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("Hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
Loading
Loading