Skip to content
Closed
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
23 changes: 19 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// Predict and explain first...
// =============> write your prediction here

// declare str again inside the function, which causes a syntax error


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

// str[0].toUpperCase() → capitalizes the first letter.

// str.slice(1) → returns the rest of the string starting from index 1.

// Combines both parts into a new string.


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

function capitalise(str) {
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good to me.

return capitalised;
}

console.log(capitalise("hello")); // "Hello"
console.log(capitalise("jesus")); // "Jesus"
18 changes: 11 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// Predict and explain first...

// We are not calling to the function from console.log, the function is convertToPercentage
// Also, its using decimalNumber outside the function, decimalnumber is a parameter it’s not defined in the global scope.

// Why will an error occur when this program runs?
// =============> write your prediction here

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


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

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

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
console.log(convertToPercentage(0.5)); // "50%"
console.log(convertToPercentage(0.25)); // "25%"

// Finally, correct the code to fix the problem
// =============> write your new code here
9 changes: 8 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@

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

function square(3) {
// It will get an error because as parameter to the function is a integer (3)
// Also num is not declare in anywhere

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

console.log(square(3));



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

// =============> explain this error message here
Expand Down
4 changes: 3 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Predict and explain first...

// The function should return a variable, the current function doesn't return anything, just console.log()

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

function multiply(a, b) {
console.log(a * b);
return(a * b);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes this is correct

}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
Expand Down
6 changes: 4 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Predict and explain first...

//The function is not returning anything

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

function sum(a, b) {
return;
a + b;
return(a+b)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes this is correct.

In JS lines can be terminated with a semicolon.

}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
Expand Down
8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Predict and explain first...

// Predict the output of the following code:

// Will be an error because we are not passing a parameter in the function getLastDigit
// the value of num is taking always the value 103, it declarate at the beginning of the program

// =============> Write your prediction here

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

function getLastDigit() {
function getLastDigit(num) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes this is a good parameter name.

return num.toString().slice(-1);
}

Expand Down
9 changes: 8 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = 0

height = height * height;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes this solution is correct, perhaps in the future we can avoid reusing variables?

It makes the code more confusing, it is good practice to use new variables:
let heightSquared = height * height;

return(weight / height).toFixed(1);
}


console.log(calculateBMI(72, 1.80));
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 toUpperSnakeCase(str) {
return str
.trim() // Remove leading/trailing whitespace
.toUpperCase() // Convert to uppercase
.replace(/\s+/g, '_'); // Replace spaces with underscores
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a nice use of regular expressions here and I like your helpful comments.

Well done.

}

console.log(toUpperSnakeCase('hello world'));
console.log(toUpperSnakeCase('lord of the rings'));
29 changes: 29 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,32 @@
// 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}`);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function behaves as expected.

Are we supposed to use a return statement instead of a console.log command?

We have a console.log command on line 33 as well.


}

console.log(toPounds('400p'));


12 changes: 7 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ 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 function is 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 value is 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The value is 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 last time we called Pad is 1, but the return is 01
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These answers are logically correct.

Be mindful of what the question is asking here,

We can rephrase the last part of the question as:
`explain why the remainingSeconds variable returns the value of 01 when inputted into the pad function?'

The return value of pad is not being asked for here.


// 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 value is returned is 01
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Explain questions ask for verbal reasoning for the reason that the return value is what it is.

Your answer could be something along the lines of:
The return value when pad is called with the input of 1 is 01 because we zero pad the front of the the number so it is 2 digits.