Skip to content
Open
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
16 changes: 14 additions & 2 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
// =============> write your prediction here
// `My prediction` => The capitalise function tries to capitalize the first character of the string and concat it with the rest. This function may not run as it has error.

// call the function capitalise with a string input
// `I called the capitalise function and found out there is a syntax error, we have already declared our 'str' variable in our function parameter, so its not a good idea to declare the same variable name as it ll throw an error.`

// interpret the error message and figure out why an error is occurring
// `The error occurs because we have declared the same (str) variable two times`. We can not declare two variables with the same name.

function capitalise(str) {
/*function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("Roman"));*/

// =============> write your explanation here
// `to make this function run we should make we should declare two different variables in our function
// .one as a parameter of the func and assign it with the above value. the second option is to return directly without creating a variable.`
// =============> write your new code here
function capitalized(str2){
let capitalizedStr = `${str2[0].toUpperCase()}${str2.slice(1)}`;
return capitalizedStr;
}
console.log(capitalized("hi dear"));
16 changes: 11 additions & 5 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// Predict and explain first...
// =====> This function tries to convert a decimal number to percentage, but I think it will not run as we have declared same variable name two times.

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> It will throw an error as we have declared decimalNumber variable in our function parameter and again we declared this decimalNumber variable inside our function.

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

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

return percentage;
}
console.log(decimalNumber);*/

console.log(decimalNumber);

// =============> write your explanation here
// =============> I tried to run this, it throw an error like: 'decimalNumber' has already been declared. to fix this we should remove the decimalNumber variable which we have inside our function.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
console.log(`Your Decimal number will be ${percentage}.`);

Choose a reason for hiding this comment

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

Can you imagine any issues that might happen by including this console.log here inside the function?

Would you want it to always print to the console every time you run this function?

return percentage;
}
convertToPercentage(0.9);
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

// Predict and explain first BEFORE you run any code...
// ====> As my understanding this function will throw and error as we have have not called the function with the argument 'num' .

// this function should square any number but instead we're going to get an error
// this function should square any number but instead we're going to get an error.
// ====> it will not square any number but 3 when we call it with the argument 'num' inside.

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

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

// =============> write the error message here
// the error message is: SyntaxError: Unexpected number.

// =============> explain this error message here
// after running I understood that we can not put a value in function parameter but a variable name. to make it short, in JavaScript we put variable in parameter and value as argument when calling the function.

// Finally, correct the code to fix the problem

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


18 changes: 12 additions & 6 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
// =============> This function will not execute any operation, as it does not return anything.

function multiply(a, b) {
console.log(a * b);
}
// function multiply(a, b) {
// console.log(a * b);
// }

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// After running the code, I found that the function works partially, it prints lines 6 and as we called the function on line 9. but line 9 has an error to be fixed. As it does not print what we need.
// also : In JavaScript, if a function doesn’t explicitly return something, it automatically returns. this is why we have got the first input ok and the second input Undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> 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)}`);
12 changes: 8 additions & 4 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Predict and explain first...
// =============> write your prediction here
// =============> This function will not return the sum of a + b as the operation is located on the next line or return. so we ll have a syntax error.

function sum(a, b) {
/*function sum(a, b) {
return;
a + b;
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// =============> after running the code I got undefined on the output as our function does not return anything. because after return keyword our function do not operate the codes.

// =============> write your explanation here
// 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)}`);
30 changes: 20 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> In my knowledge only the output from line 6 will be printed correctly. As for line 12, 13, and 14 it will throw error or will print undefined as we don't declare any variable for them but only value.

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// function getLastDigit() {
// return num.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)}`);
// 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
// =============> the output after running the code is: ---The last digit of 42 is 3 --- The last digit of 105 is 3 --- The last digit of 806 is 3;
// my prediction was half correct as it worked only with line 6 but it did not throw error or undefined. here not syntax error but logical error.

// Explain why the output is the way it is
// =============> write your explanation here
// =============> Its because JavaScript only calls the function 3 times, it does not read or ignore the value inside as they are not declared.

// 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)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// ===> it was not working with line 12, 13, and 14 because we had not declared the variable inside the function parameter, to fix the issue is to declare the num variable inside the parameter of the function. and remove the const variable of num above the function or line 6.
8 changes: 6 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,9 @@
// 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
}
let heightSquare = height * height;
let bmi = weight / heightSquare;
console.log(`Your BMI is ${bmi.toFixed(1)}`)

Choose a reason for hiding this comment

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

See my earlier comment. Leaving console.log statements inside functions might not be the best approach

return bmi;
}
calculateBMI(65, 1.73); // ==> 21.7
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 @@ -10,6 +10,12 @@
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
function upperSnakeCase(str){
let snakeCase = str.replaceAll(" ", "_").toUpperCase();
console.log(snakeCase);
return snakeCase;
}
upperSnakeCase("the vampire dairies"); // "THE_VAMPIRE_DAIRIES"

// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
Expand Down
17 changes: 16 additions & 1 deletion Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@

// You will need to take this code and turn it into a reusable block of code.
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs
function toPounds(penceString) {
const penceStringWithoutTrailingP =
penceString.substring(0, penceString.length - 1);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);

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

console.log(`£${pounds}.${pence}`);
}
toPounds("399p"); // ===> £3.99
toPounds("488p"); // ===> £4.88
toPounds("999p"); // ===> £9.99
14 changes: 8 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

function pad(num) {
return num.toString().padStart(2, "0");
}
Expand All @@ -7,28 +8,29 @@ function formatTimeDisplay(seconds) {
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

console.log(`${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`);
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
formatTimeDisplay(61); // ===> 00:01:01

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad function will be called `3` times.

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> the num value is '0' when pad is called for the first time.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> the return value of num is '00' when pad is called for the first time.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> the value of num is '1' when pad is called for the last time. to add more remainingSeconds = 61 % 60 = 1. this is why we have 1 for num value.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> the return value of num is '01' when pad is called last time. in short, remainingSeconds = 61 % 60 = 1 as num equals 1 then we have 0 from padStart(2, "0"); so the result will be "01".
30 changes: 27 additions & 3 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// This is the latest solution to the problem from the prep.
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
// --- > Debugging this code was not easy for me, I could solve this by the help of AI Explaining each step and conditions and finally we made it work well with different inputs, I mean string with number character.

Choose a reason for hiding this comment

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

Using AI to help at this stage can help you get a feel for the javascript, you might want to try doing this again by yourself - with no AI help - to make sure you learned the steps needed. For now though, the solution you have found works well.


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = time.slice(2);

let formattedHours;

if (hours === 0) {
formattedHours = 12;
} else if (hours > 12) {
formattedHours = hours - 12;
} else {
formattedHours = hours;
}

// ---> Add leading zero if needed
if (formattedHours < 10) {
formattedHours = "0" + formattedHours;
}

if (hours >= 12) {
return `${formattedHours}${minutes} pm`;
} else {
return `${formattedHours}${minutes} am`;
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
Expand All @@ -23,3 +42,8 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");
console.assert(formatAs12HourClock("13:05") === "01:05 pm");
console.assert(formatAs12HourClock("01:05") === "01:05 am");
console.assert(formatAs12HourClock("23:59") === "11:59 pm");