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
12 changes: 12 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// The program will give an error.The error happens because `str`is written twice inside the function.


// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +12,14 @@ function capitalise(str) {
}

// =============> write your explanation here
// The function already has an input called `str`
// Inside the function, the code tries to create another variable called `str`using `let`
//JavaScript does not allow the same varible name to be created again in same place.
//Because of this, the program throwns an error.

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

function captialise(str){
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
13 changes: 13 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// The program will give an error because of the `decimalNumber` is used outside the function.
// Even though it is only created inside the function.

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

function convertToPercentage(decimalNumber) {
Expand All @@ -16,5 +19,15 @@ console.log(decimalNumber);

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

//The variable `decimalNumber`is only insdie the function.
// At the end of the program, console.log( decimalNumber) tries to print it ,
// however, JavaScript cannot find that varible outside the function.
// Because of this, the program throws an error.

// 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));
11 changes: 10 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// Predict and explain first BEFORE you run any code...
// The program will give a SyntazError because a number (3) is used as peramter name in the function.

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

// =============> write your prediction of the error here
// My perdiction is that program will not run and show a SyntaxError because 3 is not a valid variable name.

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

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// Funtion parameters must be variable names.
// You cannot use a number like 3 as name of parameter.
//Javascript required a valid varible name inside the parentheses.

// Finally, correct the code to fix the problem

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


console.log(square(3));
12 changes: 12 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Predict and explain first...

// =============> write your prediction here
// The program will print 320 first.
// Then it will " The result of mutiplying 10 and 32 is undefined".


function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +12,15 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The function multiply is used console.log(a * b) to show the result
// however, it does not resturn the value .
// when the function is used inside the sentence with ${multiply(10, 32)}
// JavaScript expects the function to give back a value.
//Becuse the function does not return anything, the result becomes 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)} `);
Comment on lines +23 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is off.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?

8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
//My perdiction the code will show "undefined" becuse the function does not return the sum numbers.

function sum(a, b) {
return;
Expand All @@ -9,5 +10,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The return statemment is ematy and the "c=b" is written after return.
//When JavaScript see return it stops the function, so a+b is never used.

// 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)}`);
16 changes: 15 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
//The code will always show 3 as the last digit because the function does not use the number inside getLastDigit().

const num = 103;

Expand All @@ -15,10 +15,24 @@ 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 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 does not take any input parameter, it always uses the global variavle num = 103.
// Because of this the last digit returend is always 3, regardless of the number passed in the function call.

// 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 $(getlastDigita(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
// The function was fixed by adding a parameter so it can return the last digit of the given number.
9 changes: 7 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,10 @@
// 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
}

function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return Number(bmi.toFixed(1));
}
4 changes: 4 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,7 @@
// 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 convertToUpperSnakeCase(str) {
return str.toUpperCase().trim().split("").join("_");
Copy link
Contributor

Choose a reason for hiding this comment

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

This works.

Could also consider using the string's methods .replaceAll() or replace().

}
5 changes: 5 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,8 @@
// 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(kilograms) {
const pounds = kilograms * 2.20462;
return Number(pounds.toFixed(2));
}
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

In this exercise, the objective is to turn the "pence string to British pounds" code from an exercise in Sprint-1 into a function.

Can you update this implementation accordingly?

6 changes: 5 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// Pad wil be called 3 times because the return statement uses pad for hours, mintues and 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 value is 0 because totalHours is 0 when the input is 61 seconds.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value is "00" because padStart makes the number two digits by adding a 0 at the start.

// 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 is 1 because the last call to pad uses remainingSeconds, which is 1 when the input is 61.
// 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 is "01" because padStart adds a 0 infornt of the single digit number.
Loading