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
18 changes: 17 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Predict and explain first...
// Predict and explain first...
//Syntax error
// =============> write your prediction here
// We pass str as a parameter to the function.
//The parameter becomes a local variable inside the function scope.
//Therefore, we cannot declare another variable with the same name inside the same scope.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +14,16 @@ function capitalise(str) {
}

// =============> write your explanation here
// The parameter str is already a local variable inside the function.
// Previously, redeclaring it using let caused an error because variables cannot be declared twice in the same scope.
// Instead of redeclaring it, I modified the existing str variable and returned it.
// This avoids the redeclaration error and the code runs correctly.
// =============> write your new code here
function capitalise(str) {
const capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalised;
}
let str = capitalise("Arun");
console.log(str);


24 changes: 22 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Predict and explain first...

// syntax error identifier decimalNumber be already declare
// there is no function call and then you can't use the local variable decimalNumber out of the function
// Why will an error occur when this program runs?
//decimalNumber is already declared as a parameter, so declaring it again inside the function causes an error.

//There is no function call.

//You cannot use the local variable decimalNumber outside the function scope.
// =============> write your prediction here

// Try playing computer with the example to work out what is going on
Expand All @@ -11,10 +17,24 @@ function convertToPercentage(decimalNumber) {

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
//The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error.

//To fix this, I removed the variable keyword and modified the existing parameter instead.

//I also removed the unused variable from console.log because it was outside the function scope.

//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value.
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {

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

return percentage;
}

const percentageValue = convertToPercentage(0.5) ;
console.log(percentageValue);
24 changes: 24 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@

// Predict and explain first BEFORE you run any code...

// The code has a syntax error because a number is used instead of a parameter name in the function definition.
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
// If the function is not called, it will not execute.
// this function should square any number but instead we're going to get an error

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

// The code has a syntax error because a number is used instead of a parameter name in the function definition.
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
// If the function is not called, it will not execute.

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



// =============> write the error message here
// syntax error :unexpected number.

// =============> explain this error message here
// The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value.

// Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope.

// Also, if the function is not called, it will not execute.

// Finally, correct the code to fix the problem
// I corrected the function by properly defining num as a parameter instead of using a number in the function definition. This resolved the syntax error.

// Inside the function, num is now defined, so the ReferenceError is fixed.

// I then called the function with an argument and stored the returned value in a variable named num. Although the same variable name is used, the function parameter and the outer variable exist in different scopes, so there is no conflict.
// =============> write your new code here


function square(num) {
return num * num;
}
const squaredValue = square(3);
console.log(squaredValue);
27 changes: 26 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
// Predict and explain first...

// =============> write your prediction here
// The function multiplies the numbers and prints the answer.

// But it does not return the answer.

// Because there is no return, the function automatically gives back undefined.

// So when we use the function inside the second console.log, the value is undefined.

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
// console.log and return are not the same.

// console.log only prints the value to the console.

// It does not send the value back to the function call.

// Because there was no return statement, the function automatically returned undefined.

// Finally, correct the code to fix the problem
// To fix the issue, I replaced console.log with return.

// By returning a * b, the function now sends the calculated value back to where it was called.

// As a result, the correct value is displayed instead of undefined.
// =============> 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)}`);
14 changes: 13 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Predict and explain first...
// =============> write your prediction here

// The function returns nothing because the return statement has no value.
// Once JavaScript reaches return, the function stops executing.
// Therefore, the line after it is dead code and never runs.
// Since the function does not return a value, it returns undefined, which is why the template string displays undefined.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +12,14 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The function now correctly returns the result of a + b.
// Since the addition is included in the return statement, the function sends back the calculated value.
// Therefore, when the function is called inside the template string, it displays the correct result, 42.
// 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)}`);
37 changes: 37 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output will be 3 for every function call.
// This is because the function does not have a parameter, so it ignores the values passed in the function calls.
// Instead, it uses the global variable num, which is set to 103.
// The last digit of 103 is 3, so the function always returns 3.
// variable num inside function take the input from the global variable num in the outside of the function

const num = 103;

Expand All @@ -15,10 +20,42 @@ 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 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 getLastDigit does not have a parameter.
// Even though values like 42, 105, and 806 are passed when calling the function, they are ignored.

// Inside the function, the variable num refers to the global variable num, which is set to 103.

// Each time the function runs, it converts 103 to a string and extracts the last character using slice(-1).

// The last digit of 103 is "3".

// Therefore, every function call returns "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)}`);
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
// In the previous version, the function was not working properly because it used a global variable instead of a parameter.
// Since the function did not define a parameter, it ignored the values passed during the function calls and always used the same global value.

// In the corrected version, the global variable was removed and a parameter num was added to the function definition.
// Now, the function receives the argument passed during each function call and correctly returns the last digit of that number.

// Therefore, the output becomes:

// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 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 bmi = weight / (height * height);
bmi = bmi.toFixed(1);
return Number(bmi);

}
console.log(calculateBMI(62 , 1.8));
10 changes: 10 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,13 @@
// 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 upperSnakeCase(str){
str.toUpperCase()

return str.toUpperCase().replaceAll(" " , "_");
;

}
console.log(upperSnakeCase("hello there"));
console.log(upperSnakeCase("lord of the rings"));
19 changes: 19 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@
// 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");
return `£${pounds}.${pence}`;
}
console.log(toPounds("399p"));
console.log(toPounds("10p"));
console.log(toPounds("5p"));
31 changes: 20 additions & 11 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,36 @@ function formatTimeDisplay(seconds) {
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
console.log(formatTimeDisplay(61))

// You will need to play computer with this example - use the Python Visualizer 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

// Call formatTimeDisplay with an input of 61, now answer the following:
// ans: 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

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here : 0
// c) What is the return value of pad is called for the first time?
// =============> write your answer here value : 00

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

// =============> write your answer here : 1 When formatTimeDisplay(61) is called, the value 61 is passed as the argument to seconds.
// Then remainingSeconds is calculated using seconds % 60, which gives 1.
// The last call to pad is pad(remainingSeconds).
// Since remainingSeconds is 1, the value assigned to num in the last call is 1.
// 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
// =============> write your answer here : 01 When formatTimeDisplay(61) is called, the value 1 is passed to pad during the last call because remainingSeconds equals 1.
// Inside the pad function:
// The number 1 is converted to a string.
// padStart(2, "0") ensures the string has at least two characters.
// Since "1" has only one character, a "0" is added to the beginning.
// So "1" becomes "01".
// This ensures the time format follows the 00:00:00 structure.
24 changes: 23 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const convertedHours= hours - 12;
return `${convertedHours.toString().padStart(2 , "0")}:00 pm`
}
return `${time} am`;
}
Expand All @@ -23,3 +24,24 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
const currentOutput3 = formatAs12HourClock("19:00");
const targetOutput3 = "07:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("21:00");
const targetOutput4 = "09:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);



// The previous version only worked properly for two-digit hours. To fix this, I made sure the hour is always displayed in two-digit format.

// First, I completed the mathematical calculation and stored the result in a variable. Then, I converted it to a string and used padStart(2, "0") to add a leading zero when needed.

// Now, it works correctly for all times greater than 12 and always shows two digits.