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
21 changes: 15 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here:
// there is promblem declaring the string twice.

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

//}

// =============> write your explanation here: It say's syntax error "str" has been declared twice. We can change variable name in tp text or anything else

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

function capitalise(text) {
let capitalisedText = `${text[0].toUpperCase()}${text.slice(1)}`;
return capitalisedText;
}
console.log(capitalise("hello world"));
23 changes: 15 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here: const decimalNumber = 0.5; this line should be declare outside of function

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

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

return percentage;
}
//return percentage;
//}

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

// =============> write your explanation here
// =============> write your explanation here: In line 9 const decimalNumber = 0.5; should not be in their because it will redeclare in side function which will be error, it will be local scope if its inside a function. declaring outside of code make global scope, can call through out the code.

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

}
console.log(decimalNumber);
19 changes: 12 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

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

// =============> write your prediction of the error here
// =============> write your prediction of the error here: because parameter should use as num .

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

// =============> write the error message here: syntax error: unexpected number.

// =============> write the error message here
// =============> explain this error message here: We cannot use parameter as num like 3 or 4 in inside function as argument we should use num, so we can re call many times through out the code.

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

// Finally, correct the code to fix the problem

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


function square(num){
return num * num;
}
console.log(square(3));
console.log(square(4));
14 changes: 8 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Predict and explain first...

// =============> write your prediction here
// =============> write your prediction here: there is no return statement

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
// =============> write your explanation here;
//multiply function is logged with out define so the result will be undefined, need add return statement to complete it.

// 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)}`);
16 changes: 10 additions & 6 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: there is ; error



// =============> write your explanation here: function will be undefined because of ";" by removing ; placing a+b in same line will return function.
Copy link
Contributor

Choose a reason for hiding this comment

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

Between "return value" and "function", which one is undefined?

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return;
a + b;
return a+b;

}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
//Between "return value" and "function", which one is undefined?

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
//return value will be undefined if function have not return statement it will automatically returns undefined.
26 changes: 20 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
// Predict and explain first...
// Predict and explain first...because of const num, it won't change output.

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here: 3

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
// =============> write the output here: 3 for all console.log.
// Explain why the output is the way it is
// =============> write your explanation here
// =============> write your explanation here: 3 for all of the the console.log because it won't take any argument it will always take global variable num = 103.
// 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 ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// Does the function toPounds() return the last digit of the given parameter?
// No, it doesn't return toPounds() of the last digit of the given parameter.




// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
//because of line 6.
8 changes: 7 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,10 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
}
function calculateBMI(weight, height){
let bmi = weight /(height * height);
return parseFloat(bmi.toFixed(1));
}

console.log(calculateBMI(70, 2.99));
5 changes: 5 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,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
const text = `hello there`;
console.log(text.toUpperCase().replace(/ /g, "_"));

const sentence = `lord of the rings`;
console.log(sentence.toUpperCase().replace(/ /g, "_"));
21 changes: 21 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,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(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(`The last digit of 399p is ${toPounds("399p")}`);
console.log(`The last digit of 52p is ${toPounds("52p")}`);
console.log(`The last digit of 1099p is ${toPounds("1099p")}`);
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 @@ -10,25 +10,27 @@ 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
// =============> 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
// =============> write your answer here: 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 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
// =============> write your answer here: 1, when pad is called for the last time, pad(remainingSecond) is remainingSecond=1

// 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
// =============> write your answer here: It will be 01 as we used padStart(2, "0") which will make 2 digit for example if value was 1 it will return as 01.