Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e1447e3
Increase by 1 the variable count
carlosyabreu Nov 3, 2025
f184b29
Assignment operator equal
carlosyabreu Nov 6, 2025
d88693e
2-initials.js first letters of the variables
carlosyabreu Nov 6, 2025
ad07e16
Directory and extension part of a file path
carlosyabreu Nov 6, 2025
fc159be
Explanation of 4-random.js file
carlosyabreu Nov 6, 2025
92d1f33
Using comment to explain the information contain in the file, useful …
carlosyabreu Nov 6, 2025
204a98d
Change keyword 'const' to 'let' to assign variable
carlosyabreu Nov 6, 2025
b9538b2
Variable must be declared first before it can be used
carlosyabreu Nov 6, 2025
5b6a38f
Extracting the last 4 digits and display it
carlosyabreu Nov 7, 2025
57e97e9
Change variable naming starting with letter instead of numbers
carlosyabreu Nov 7, 2025
7e75245
Change function replaceAll arguments adding commas to separate its pa…
carlosyabreu Nov 7, 2025
a36e485
commit 2-time-format.js and 3-to-pounds.js
carlosyabreu Nov 7, 2025
94b7128
Add alert and prompt function explanation
carlosyabreu Nov 7, 2025
269ce23
Console.log and Console and property
carlosyabreu Nov 7, 2025
4818ddd
Correct a function with let defined inside it
carlosyabreu Nov 7, 2025
992f376
Change the local variable to be called from global scope
carlosyabreu Nov 7, 2025
607ca58
Change a literal numerical value as parameter for a variable expect b…
carlosyabreu Nov 7, 2025
79b52ed
Commit 0.js and 1.js files
carlosyabreu Nov 7, 2025
6b17b68
Change the function to display the last digit correctly
carlosyabreu Nov 7, 2025
e29954a
Correct the BMI function and the necessary explanation
carlosyabreu Nov 7, 2025
9577ac5
Change small letter into capital letter
carlosyabreu Nov 7, 2025
2b746b1
Different display of pounds
carlosyabreu Nov 7, 2025
886d971
Test of function at different scenarios
carlosyabreu Nov 7, 2025
d34a28d
Display time in usual format
carlosyabreu Nov 7, 2025
da83341
Delete Sprint-1 folder
carlosyabreu Nov 8, 2025
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
4 changes: 0 additions & 4 deletions .gitignore

This file was deleted.

27 changes: 24 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
// Predict and explain first...
// =============> write your prediction here

// Predict and explain first...
// =============> write your prediction here
// This code will throw an error: "SyntaxError: Identifier 'str' has already been declared".
// The reason is that the variable 'str' is declared twice — once as a function parameter
// and again with `let str` inside the function. You cannot redeclare a parameter variable
// using `let` or `const` in the same scope.

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

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

// The error occurs because 'str' is already defined as a parameter to the function.
// Using 'let str' again inside the function tries to redeclare it, which is not allowed.
// We can fix this by either using a different variable name or by reassigning 'str'
// directly without redeclaring it.

// =============> write your new code here
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your new code here
// Example:
console.log(capitalise("hello")); // Output: "Hello"

54 changes: 54 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// The code will throw a "SyntaxError: Identifier 'decimalNumber' has already been declared"
// because the variable 'decimalNumber' is declared twice — once as a function parameter
// and again with `const decimalNumber` inside the function.
// Additionally, there will be a "ReferenceError: decimalNumber is not defined"
// when trying to log it outside the function, since 'decimalNumber' only exists inside the function 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}%`;
Expand All @@ -13,8 +20,55 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/



// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// The code will throw a "SyntaxError: Identifier 'decimalNumber' has already been declared"
// because the variable 'decimalNumber' is declared twice — once as a function parameter
// and again with `const decimalNumber` inside the function.
// Additionally, there will be a "ReferenceError: decimalNumber is not defined"
// when trying to log it outside the function, since 'decimalNumber' only exists inside the function 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

// The first error happens because 'decimalNumber' is redeclared inside the function
// even though it’s already defined as a parameter. JavaScript does not allow
// redeclaring parameters with `let` or `const`.
// The second error happens because 'decimalNumber' is not defined in the global scope
// — it only exists inside the function, so `console.log(decimalNumber)` cannot find it.

// 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)); // Output: "50%"
*/

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

console.log(convertToPercentage(0.5)); // Output: "50%"

16 changes: 15 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// This code will throw a SyntaxError because function parameters must be valid variable names,
// not literal values like 3.

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

// =============> write the error message here
// SyntaxError: Unexpected number
// (or "SyntaxError: Unexpected token '3'" depending on the environment)

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

// The error occurs because JavaScript expects a variable name (like num or x) inside the parentheses
// of a function definition. You cannot use a number (3) as a parameter name — it’s not a valid identifier.
// Function parameters act as variable placeholders for input values, so they must follow variable naming rules.
//
// Finally, correct the code to fix the problem

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

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

console.log(square(3)); // Output: 9

23 changes: 22 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
// Predict and explain first...
// The console will print:
// 320
// The result of multiplying 10 and 32 is undefined

// =============> write your prediction here
// The first `console.log` inside the function prints the multiplication result (320).
// However, since `multiply()` does not return anything, the outer `console.log`
// receives `undefined` as the value of the function call, leading to the message
// "The result of multiplying 10 and 32 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

// The first `console.log` inside the function prints the multiplication result (320).
// However, since `multiply()` does not return anything, the outer `console.log`
// receives `undefined` as the value of the function call, leading to the message
// "The result of multiplying 10 and 32 is undefined".
//
// To fix this, the function should `return` the result instead of just logging it.
// 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)}`);
// Output: "The result of multiplying 10 and 32 is 320"
20 changes: 20 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
// Predict and explain first...
// =============> write your prediction here
// The console will print: "The sum of 10 and 32 is undefined"
// This happens because the `return` statement ends the function immediately,
// so the expression `a + b` after it is never executed.
// As a result, the function returns `undefined`.

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


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

// =============> write your explanation here
// In JavaScript, when the interpreter encounters a `return` statement,
// it stops executing the rest of the function and immediately exits.
// Since there’s nothing after `return` on the same line,
// the function effectively returns `undefined`.
// The expression `a + b` is never reached due to JavaScript’s 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 and 32 is ${sum(10, 32)}`);
// Output: "The sum of 10 and 32 is 42"

37 changes: 28 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output will be:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
//
// This happens because the function `getLastDigit` doesn’t use the argument passed to it.
// Instead, it always refers to the constant `num = 103`,
// so every time you call it, it converts 103 to a string and returns the last digit ("3").

const num = 103;
// 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` ignores the argument being passed (like 42, 105, or 806)
// because it doesn’t have a parameter defined.
// Instead, it always uses the globally defined constant `num` (which is 103).
// Therefore, the function always returns the last digit of 103, which is "3".

function getLastDigit() {
// 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)}`);

// 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
// The function was not working because it didn’t accept any parameters and
// was instead using the fixed global variable `num`.
// By adding a parameter to the function and using that instead,
// it now correctly calculates the last digit for any input number.

30 changes: 29 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@
// Then when we call this function with the weight and height
// 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
}
}
*/

/**
This function works by:

Squaring the height.

Dividing the weight by the squared height.

Using toFixed(1) to round the BMI to 1 decimal place and converting it back to a number.
*/

// Function to calculate BMI
function calculateBMI(weight, height) {
// Calculate BMI: weight divided by height squared
const bmi = weight / (height * height);

// Round the result to 1 decimal place and return
return Number(bmi.toFixed(1));
}

// Example usage:
console.log(calculateBMI(70, 1.73)); // Output: 23.4
console.log(calculateBMI(60, 1.6)); // Output: 23.4
console.log(calculateBMI(90, 1.8)); // Output: 27.8


22 changes: 22 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,25 @@
// 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 to convert a string to UPPER_SNAKE_CASE
function toUpperSnakeCase(str) {
// Replace spaces with underscores and convert to uppercase
return str.replace(/\s+/g, "_").toUpperCase();
}

// Example usage:
console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS"
console.log(toUpperSnakeCase("javascript is fun")); // Output: "JAVASCRIPT_IS_FUN"
Copy link
Contributor

Choose a reason for hiding this comment

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

What return value do you expect from the following function call?

toUpperSnakeCase("hello   there");  // three spaces between the two words

Copy link
Author

Choose a reason for hiding this comment

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

If the function toUpperSnakeCase() converts a string to UPPER_SNAKE_CASE then:

  1. Replace spaces (or word separators) with underscores _.
  2. Convert all letters to uppercase.

Because the function doesn't require the multiple white space to normalise (i.e. to collapse into one) then we might get multiple underscores resulting in the following output:

"HELLO___THERE"

Copy link
Contributor

Choose a reason for hiding this comment

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

Does your function return "HELLO___THERE" when the parameter is "hello there"?


/**
Explanation:
str.replace(/\s+/g, "_") replaces all whitespace (spaces, tabs, etc.) with underscores.
.toUpperCase() converts the resulting string to uppercase letters.
The function returns the final string in UPPER_SNAKE_CASE
*/

53 changes: 53 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,56 @@
// 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

/**
let penceString = "399p";

let penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

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

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

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

// Function to convert a pence string (like "399p") to pounds
function toPounds(penceString) {
// Remove the trailing 'p'
let penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// Pad the string to ensure at least 3 digits
let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Extract pounds
let pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// Extract pence
let pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

return `£${pounds}.${pence}`;
}

// Example usage:
console.log(toPounds("399p")); // Output: £3.99
console.log(toPounds("5p")); // Output: £0.05
console.log(toPounds("1234p")); // Output: £12.34
console.log(toPounds("50p")); // Output: £0.50

Loading
Loading