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
20 changes: 16 additions & 4 deletions Sprint-3/1-key-errors/0.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here
// =============>// I predict JavaScript will give a SyntaxError because str is declared twice in the function.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
capitalise("hello");*/
/* =============> The error happens because str is already the function's parameter.
let str tries to use the same name again, so JavaScript gives a SyntaxError.
The function stops before it can run. */
// =============> write your new code here
function capitalise(str) {
if (str === "") {
return "";
}

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

console.log(`Result: "${capitalise("")}"`);
17 changes: 13 additions & 4 deletions Sprint-3/1-key-errors/1.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============>// I predict a SyntaxError because decimalNumber is declared as a parameter and then declared again with const.

// 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
/* =============> decimalNumber is already the function parameter.
const decimalNumber tries to declare the same name again inside the function,
so JavaScript gives a SyntaxError. */

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

return percentage;
}

console.log(convertToPercentage(0.5));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done fixing the original error by removing the redeclared const decimalNumber, and your code runs correctly; just note that toFixed(1) changes the output from 50% to 50.0%, so either remove it to match the expected result or add a short comment explaining why you chose to include it.

17 changes: 9 additions & 8 deletions Sprint-3/1-key-errors/2.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

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

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

// =============> write your prediction of the error here
// =============> // I predict a SyntaxError because 3 is a number, but a function parameter needs to be a name.

function square(3) {
/*function square(3) {
return num * num;
}
*/
// =============> SyntaxError: Unexpected number

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

// =============> explain this error message here
// =============> The error happens because 3 is not a name. A parameter needs a name, like num.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3));
14 changes: 9 additions & 5 deletions Sprint-3/2-mandatory-debug/0.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Predict and explain first...

// =============> write your prediction here
// =============> I couldn't predict until I run the code

function multiply(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
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/

/* =============> The Terminal gives an answer of 320 but also says "The result of multiplying 10 and 32 is undefined"
which makes me think that the function has no return so it gives back undefined.*/
// 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)}`);
14 changes: 10 additions & 4 deletions Sprint-3/2-mandatory-debug/1.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I predict the code won't work because there's a ";" after return.

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

// =============> write your explanation here
/* =============> The sum is undefined because the semicolon after return ends it too early,
the function returns before adding a and b.*/
// 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)}`);
29 changes: 24 additions & 5 deletions Sprint-3/2-mandatory-debug/2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot on! Adding num as a parameter fixed the scoping issue completely.

  • What happens if you pass a negative number, like getLastDigit(-42)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If I call getLastDigit(-42), it returns "2", because the function converts the number to a string and gets its last character.

Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> I predict the result will be wrong because the num is set set on 103.

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

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 806 is ${getLastDigit(806)}`);*/

// Now run the code and compare the output to your prediction
// =============> write the output here
/* =============> the output:
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 problem is that the function ignores the numbers passed to it,
it always uses num, which is 103, so it returns 3 every time.*/
// Finally, correct the code to fix the problem
// =============> write your new code here
/*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)}`);*/
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// In this case getLastDigit is not working because it's parameter is empty and not declared.
// Here's the right code:
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)}`);
3 changes: 2 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
return (weight / (height * height)).toFixed(1);
}
console.log(calculateBMI(52, 1.63));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The exercise asks for the function to be implemented and checked with several inputs, ensuring the function works across multiple inputs. You can add a few more test checks to demonstrate this.

5 changes: 5 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
// 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(words) {
return words.toUpperCase().trim().split(/\s+/).join("_");
}

console.log(toUpperSnakeCase("hello there"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work, your function correctly converts the string to UPPER_SNAKE_CASE, and using trim() with split(/\s+/) is a thoughtful way to handle extra spaces; you can add a few more test examples, such as a single word like "hello", and a string with extra spaces to show it works for a range of inputs

17 changes: 17 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spot on! This is a clever way to handle string slicing and padding to format the currency correctly.

  • What happens if someone passes "599" without the trailing "p", or if they pass a number like 599 directly? How could you make the function flexible enough to handle those cases?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Without the p, it removes a digit by mistake. I can convert the input to a string and remove the p only when it’s there.

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,20 @@
// 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 penceNumberString = String(penceString).replace(/p$/i, "");
const paddedPenceNumberString = penceNumberString.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("599p"));
console.log(toPounds("6p"));
console.log(toPounds("2568p"));
12 changes: 6 additions & 6 deletions Sprint-3/4-mandatory-interpret/time-format.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(61));

// 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 will be called 3 times: once for hours, once for minutes, and once for seconds.

// 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 first time pad is called, num gets the value of totalHours, which is 0.

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

// =============> the first time pad is called, it returns 00 because num is 0 and pad adds a zero in front.
// 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 last time pad is called, num is 1 because there is 1 second left.

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============>the last time pad is called, it returns 01 because num is 1 and pad adds a 0 in front.
50 changes: 38 additions & 12 deletions Sprint-3/5-stretch-extend/format-time.js
Comment thread
fatimarouchi marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,49 @@
// 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.

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

let suffix = "am";
if (hours >= 12) {
suffix = "pm";
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
let formattedHours = hours % 12;
if (formattedHours === 0) {
formattedHours = 12;
}

return `${formattedHours}:${minutes} ${suffix}`;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work using hours % 12 with the reset to 12, which handles both midnight and noon correctly; one thing to fix is that formattedHours is a number, so 08:00 returns 8:00 am instead of 08:00 am. You look into fixing that.


const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
);

const noonOutput = formatAs12HourClock("12:00");
const noonTarget = "12:00 pm";
console.assert(
noonOutput === noonTarget,
`current output: ${noonOutput}, target output: ${noonTarget}`,
);

const midnightOutput = formatAs12HourClock("00:00");
const midnightTarget = "12:00 am";
console.assert(
midnightOutput === midnightTarget,
`current output: ${midnightOutput}, target output: ${midnightTarget}`,
);

const singleDigitMinuteOutput = formatAs12HourClock("9:5");
const singleDigitMinuteTarget = "9:05 am";
console.assert(
singleDigitMinuteOutput === singleDigitMinuteTarget,
`current output: ${singleDigitMinuteOutput}, target output: ${singleDigitMinuteTarget}`,
);

console.log("All formatAs12HourClock tests passed.");
Comment on lines +29 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job testing noon and midnight, which are the edge cases most likely to break this function. Two things to think about: what happens to your final success message if one of the assertions fails, and is "9:5" a valid 24-hour time whose expected output matches the format of your other tests?

Loading