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
16 changes: 11 additions & 5 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here

//- we expect a syntaxerror because "str" is already declared as function parameter and again as a variable within the same function scope.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
//"Error: SyntaxError: Identifier 'str' has already been declared" why: because "str" declared twice.

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

// =============> write your explanation here
//- I changed the variable name to "strg" to avoid the error.
// =============> write your new code here
function capitalise(str) {
let strg = `${str[0].toUpperCase()}${str.slice(1)}`;
return strg;
}
27 changes: 20 additions & 7 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// Predict and explain first...

// Why will an error occur when this program runs?
//- variable name "decimalNumber" declared twice in the same function. one as function parameter and again as a const in the same function.
// =============> write your prediction here

//- SyntaxError because "decimalNumber" declared twice.
// 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
// I moved `const decimalNumber = 0.5` outside the function. The function already has `decimalNumber` as a parameter, so I can pass a number directly when calling it, like `convertToPercentage(7)`. The function then multiplies it by 100 and adds `%`.

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

const decimalNumber = 0.5;

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;

const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(7));

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

// Finally, correct the code to fix the problem
// =============> write your new code here
17 changes: 12 additions & 5 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

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

// this function should square any number but instead we're going to get an error
//we are giving a value for the input instead of name that can hold different values.

// =============> write your prediction of the error here
//syntaxerror related to the function input "(3)"

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

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

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

// we gave a placeholder for the function instead of value that can't be changed.
// Finally, correct the code to fix the problem

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


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

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

// =============> write your prediction here
//- "a and b" are defined as parameters and receive 10 and 32. The function calculates and logs 320, but it doesn't return the value. Because there is no return, the function call evaluates to undefined.

function multiply(a, b) {
console.log(a * b);
}
// function 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
//- Added a return statement so the function returns the result to the function call. The console.log() is just printing 320 and function doesn't give 320 back and thats why "${multiply(10, 32)}" gets 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)}`);
18 changes: 13 additions & 5 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
//- return statement in this case stops before it can do the sum of a + b.

function sum(a, b) {
return;
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
//- added a + b to return so function returns the sum.
// 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)}`);
34 changes: 27 additions & 7 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

// Predict the output of the following code:
// =============> Write your prediction here
//-getting the last digit of 103

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

function getLastDigit() {
return num.toString().slice(-1);
}
// 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 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
//-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
// -Every time the function calls 103
// =============> 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 looking at the outside "num" but Now there are two (num) one outside the scope "const num= 103" and i added another inside the function scope.
//-The function calculates something and returns it then console.log outside receives that returned value and prints it.

const num = 103;

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


9 changes: 8 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// Then when we call this function with the weight and height
// 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
const bmi= weight / (height * height);
return bmi.toFixed(1);

// return the BMI of someone based off their weight and height
}

console.log(`Your BMI is ${calculateBMI(70, 1.73)}, based on your weight and height`);

6 changes: 6 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// 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 convertToUpperCase(sentence) {
return sentence.toUpperCase().replaceAll(" ", "_");

}
console.log(convertToUpperCase("welcome to Chad"));
23 changes: 23 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@
// 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");

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

console.log(toPounds("399p"));
console.log(toPounds("4000p"));
console.log(toPounds("39p"));
console.log(toPounds("3p"));
13 changes: 7 additions & 6 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay());
// 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

//- 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
// b) What is the value assigned to num when pad is called for the first time?
//- 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
//- 1, remainingSeconds is 1 because 61 % 60 = 1. The last call is pad(remainingSeconds) so pad(1) makes num = 1.

// 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
//- "01" , num.toString() changes 1 into "1". Since "1" has only 1 character, the while loop adds "0" in front, making "01".
Loading