Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
43a1dfd
fix: resolve naming conflict and add scope documentation
Grajales-K Jan 26, 2026
8fb443a
fix: fixed the code that was causing fix: resolve scope error and sto…
Grajales-K Jan 26, 2026
7655d2a
fix: replace numeric literal with identifier in function parameter
Grajales-K Jan 26, 2026
627e70d
fix: change console.log to return statement to avoid undefined result
Grajales-K Jan 26, 2026
e1133a2
fix: move operation to the return statement to avoid unreachable and …
Grajales-K Jan 26, 2026
4739b94
fix: add parameter to function to use passed arguments instead of a g…
Grajales-K Jan 26, 2026
44374b7
Merge branch 'CodeYourFuture:main' into coursework/sprint-2
Grajales-K Jan 27, 2026
ee55dfe
feat: add a new math operation to round a number and get 1 decimal pl…
Grajales-K Jan 30, 2026
854cdb4
feat: add new methods function to convert an string input as upperCas…
Grajales-K Jan 31, 2026
6e1b61c
refactor: reusable function code to treat string to convert to only n…
Grajales-K Feb 1, 2026
298e008
refactor: added two way to test the function to convert str to pounds
Grajales-K Feb 1, 2026
558ea37
docs: explanation how is iterating both functions when they are invok…
Grajales-K Feb 1, 2026
83d8641
Merge branch 'CodeYourFuture:main' into coursework/sprint-2
Grajales-K Feb 1, 2026
cf64d7e
Merge branch 'CodeYourFuture:main' into coursework/sprint-2
Grajales-K Feb 13, 2026
79eb762
feat: add 12-hours clock conversion, includes padding for single digi…
Grajales-K Feb 16, 2026
b13e47e
fix: the comment test line
Grajales-K Feb 16, 2026
c024fde
fix: removed line 3
Grajales-K Feb 16, 2026
8c00d6c
refactor: removed unnessary variable and return the result
Grajales-K Feb 16, 2026
c845780
refactor: convert convertToPercentage to arrow function for conciseness
Grajales-K Feb 16, 2026
71034f4
fix: removed console.log to better readability
Grajales-K Feb 16, 2026
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
23 changes: 19 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
// 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;
}
// function capitalise(str) {
// const capiLetter = `${str[0].toUpperCase()}${str.slice(1)}`;
// return capiLetter;
// }

// =============> write your explanation here

// The original code failed for two reasons: first the function was never invoke,
// there was a variable naming conflict with the function scope.
// Since str was already defined as a parameter, can not be used to re-declare
// a variable inside the function. To fix this, I moved the processed directly and
// returned the result directly

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


function capitalise(str) {

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

console.log(capitalise("hello"));
32 changes: 26 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@

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

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

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here

// This function will throw a SyntaxError because decimalNumber
// is being redeclared inside the same scope as the parameter.
// Additionally, the function ignores the input parameter
// because the value is hardcoded inside the body.
// Finally, the console.log will throw a ReferenceError
// because it tries to access a local variable from the global scope.
// To fix this, we must remove the internal declaration to use the
// parameter dynamically and invoke the function correctly.

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



// function convertToPercentage(num) {
// return `${num * 100}%`;;
// }


const convertToPercentage = (num) => `${num * 100}%`;

console.log(convertToPercentage(0.80));
20 changes: 17 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@

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

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


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

// It will throw a syntaxError because a literal Number(3) cannot be used
// as a function parameter. furthermore, the return statement will
// cause an referenceError because it attempts to use a var num that
// has no been defined. To fix this, we must replace the number in
// the function with the identifier num.

// =============> explain this error message here

// Finally, correct the code to fix the problem

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



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


console.log(square(9));
17 changes: 13 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

// =============> write your prediction here

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

// =============> write your explanation here

//will display the first console.log and make the operation 10 * 32
// but the second console.log will throw undefined because
// the function does not have a return statement.

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

function sum(a, b) {
return;
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)}`);

// =============> write your explanation here

// This function will return undefined because the return statement executes first.
// since the operation a + b is after the return statement, it will never be executed.


// 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)}`);
43 changes: 35 additions & 8 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,49 @@
// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
// The output will always be 3 for all cases. Even though
// arguments are passed to the function, it lacks parameter
// to receive them. Therefore, the function defaults to using
// the global scope constant num = 103 every time it runs.

function getLastDigit() {
return num.toString().slice(-1);
}
// const num = 103;

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

// Now run the code and compare the output to your prediction
// =============> write the output here
// The output is:
// 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 is not working properly because
// it is hardcoded to use the global constant num. Since
// the function definition does not include any parameters,
// it ignores the arguments (42, 105, 806) passed during the
// call. As a result, it always calculates the last digit of 103, which is 3

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

const num = 103; //this now is a ghost variable and is not used in the function

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

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Explain why getLastDigit is not working properly - correct the problem
17 changes: 15 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,18 @@
// 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
}
// return the BMI of someone based off their weight and height
const squareHeight = height * height;
const operation = weight / squareHeight;
const bmi = Math.round(operation * 10) / 10;

return bmi;
}


console.log(calculateBMI(70, 1.73));


// to get 23.4 we use the Math.round(operation * 10) / 10; to move the decimal
// one place to the right, round it to the nearest whole number, then move it
// back by dividing by 10.
11 changes: 10 additions & 1 deletion Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// Use the MDN string documentation to help you find a solutio
// 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"
75 changes: 75 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,78 @@
// 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


// =============================== Original Code ========================
// const penceString = "399p";

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



// =============================== Refactoring ========================

// to make robust function we can use some maths methods to reduce the looping and
// return only the numbers, like this: /[^\d]/g, with that we can remove all
// characters that are no number, then we can convert the number into decimals by
// dividing by 100.

//padStart method can be used to ensure that strings have a minimum length, no used in numbers
//The toFixed() method formats a number using fixed-point notation.

// In programming, we call this a Regular Expression (Regex). Think of it as a custom filter for text.

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions

// REGEX BREAKDOWN

// Delimiters (/ /): The "brackets" that mark the start and end of the Regex.
// Non-digit (\D): Matches any character that is NOT a number (0-9).
// Negated Set ([^\d]): The long version of \D (Anything that is NOT a digit).
// Global Flag (g): Tells the code to clean the entire string, not just the first letter.
// Empty String (''): Replaces the "bad" characters with nothing (deleting them).
// Example: "£1.50p" -> "150"
// const onlyNumbers = str.replace(/\D/g, '');

// =============================== Reusable Code ========================


function toPounds(str){

const onlyNumbers = str.replace(/[^\d]/g, '');
const decimals = (onlyNumbers/100).toFixed(2);

return `£${decimals}`;
};



// =============================== 1st way to test ========================

const examples = ["79p", "hfhbgjk1p", "150p", "£2.50", "11000"];
for (const example of examples) {
console.log(toPounds(example));
}


// =============================== 2nd way to test ========================

// const examples2 = ["79p", "hfhbgjk1p", "150p", "£2.50", "12000"];
// for (const example of examples2) {
// console.log(`Original: ${example} -> Formatted: ${toPounds(example)}`);
// };
20 changes: 17 additions & 3 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,46 @@ function pad(num) {
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const remainingSeconds = seconds % 60;
//get the remaining seconds after dividing by 61/60 = 1 remainder 1
const totalMinutes = (seconds - remainingSeconds) / 60;
//get seconds = 1 - 60; result is 60/60 = total minutes 1
const remainingMinutes = totalMinutes % 60;
//the remainingMinutes = 1 & 60 = 1; because the totalMinutes is less than
// 60 the number will always the number from left side of the modulus operator
const totalHours = (totalMinutes - remainingMinutes) / 60;

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


console.log(formatTimeDisplay(61)); // Should print "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 when formatTimeDisplay is called.

// Call formatTimeDisplay with an input of 61, now answer the following:
//==== 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
// =============> write your answer here
// The value assigned to num when pad is called for the first time is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when it is called for the first time is "00".
// because num is 0, and when we convert it to a string and pad it to 2
// characters with leading zeros, it becomes "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
// The value assigned to num when pad is called for the last time in this program 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
// The return value assigned to num when pad is called for the last time in this program is "01".
Loading