Skip to content
11 changes: 10 additions & 1 deletion Sprint-2/1-key-exercises/4-random.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.

"Nice work exploring default parameters!
Quick question: When you run console.log(\hello${greet("12:00")} how are you?), what gets printed in the console and why does undefined show up in the middle of the string?"

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const minimum = 1;
/*const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
Expand All @@ -7,3 +7,12 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing
<<<<<<< Updated upstream
=======
console.log(num)*/

function greet(timeOfDay, name="edward"){
console.log(`Good ${timeOfDay}, ${name}.`);
}
console.log(`hello${greet("12:00")} how are you?`)
>>>>>>> Stashed changes
9 changes: 7 additions & 2 deletions Sprint-3/1-key-errors/0.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.

Great fix using string1! Just a quick note: str is a valid variable name, but the error happened because it was already declared as a parameter.

Quick question: Do you need to store the result in a new variable, or could you return the template literal directly?

Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
//"str" is not a valid variable name

// 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)}`;
let str= `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your explanation heres
// =============> write your new code here
function capitalise(str) {
let string1= `${str[0].toUpperCase()}${str.slice(1)}`;
return string1;
}
17 changes: 13 additions & 4 deletions Sprint-3/1-key-errors/1.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.

Good job fixing the function body!

Quick question: When you run console.log(convertToPercentage);, what gets printed in the terminal, and how do you need to write that console.log to see the percentage result?

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

//decimal number was already declaired
//logging wrong thing
// 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

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

return percentage;
}

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

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

// 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);
9 changes: 6 additions & 3 deletions Sprint-3/1-key-errors/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.

"Great job fixing the parameter name!

Quick question: How would you test this function by calling it and logging the result to the console?"

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

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

// =============> write your prediction of the error here
// =============> num is not defied

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

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

Expand All @@ -16,5 +16,8 @@ function square(3) {
// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


12 changes: 9 additions & 3 deletions Sprint-3/2-mandatory-debug/0.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.

"Great job fixing that to use return instead of console.log!
Quick question: Do the extra parentheses around (a * b) change how the code behaves, or are they optional?"

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

//used log instead of return in fuction
//
// =============> write your prediction here

function multiply(a, b) {
/*nction multiply(a, b) {
console.log(a * b);
}

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

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

// 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)}`);
8 changes: 8 additions & 0 deletions Sprint-3/2-mandatory-debug/1.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.

"Great job fixing that! Moving a + b onto the same line as return makes the function work correctly.

Quick question: What happens if someone calls sum("10", "32") with strings instead of numbers, and why does JavaScript do that?"

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
//the return atement was closed without executing anything

function sum(a, b) {
return;
Expand All @@ -11,3 +12,10 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// =============> write your explanation here
// 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)}`);
1 change: 1 addition & 0 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.

Take a close look at where num comes from inside getLastDigit.

Quick question: How can you update the function definition so it accepts an argument and uses the number passed to it instead of the global num = 103?

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Predict the output of the following code:
// =============> Write your prediction here
//no error to my accord

const num = 103;

Expand Down
2 changes: 2 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.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.

Let's break down the formula step by step:

  1. Squaring the height: In JavaScript, the ^ symbol is a bitwise operator, not a power exponent. To square a number, you can use height * height or height ** 2.
  2. The operation: The instructions state to divide the weight by the squared height, rather than multiplying.
  3. Decimal places: To format the result to 1 decimal place as requested, you can use the .toFixed(1) method.

What would the code look like if you combine division, squaring, and .toFixed(1)?

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const BMI=`BMI:${height^2}*${weight}`
return BMI
// return the BMI of someone based off their weight and height
}
3 changes: 3 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.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.

"Great job! Using regular expressions (/ /g) with replace and toUpperCase is a very clean solution.
Quick question: What happens if there are multiple spaces between words, like "hello there", and how could we handle that?"

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
function toUpperSnakeCase(str) {
return str.replace(/ /g, "_").toUpperCase();
}

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

Expand Down
21 changes: 21 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.

"Great job turning this into a working, reusable function and testing it with "900p"!
Quick question: Look at the first line (let penceString = string;). Is that extra variable necessary, or could you use the function parameter directly?"

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
// 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(string){
let penceString = string;

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("900p"))
9 changes: 8 additions & 1 deletion Sprint-3/4-mandatory-interpret/time-format.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.

"Almost there! Let's check a couple of small things:

  1. Syntax check: Look closely right after the pad function—it's missing a closing curly brace } before formatTimeDisplay starts, which will cause a syntax error.
  2. Question (d): When pad is called for the last time (for the seconds), the value assigned to num is 1 (since 61 % 60 = 1).
  3. Question (e): The return value of that specific call to pad(1) is "01". (Note: "00:01:01" is the final result of the whole formatTimeDisplay function, whereas pad only returns the formatted piece for that single call).

Try adding the missing closing brace and running your console.log(formatTimeDisplay(61))—what does it output in the terminal?"

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ function pad(num) {
numString = "0" + numString;
}
return numString;
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
Expand All @@ -22,17 +21,25 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
//3

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

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
//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
//01
//

// 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
//00:01:01
console.log(formatTimeDisplay(61))
Loading