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
11 changes: 7 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Predict and explain first...
// =============> write your prediction here
// The issue might be that the input to the function is str but also there is a variable declaration str inside it

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
// =============> write your new code here
// Uncaught SyntaxError: Identifier 'str' has already been declared - meaning str should not have been used inside the function or the parameter of the function
function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
17 changes: 12 additions & 5 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
// =============> same as before, decimalNumber in the function para, but also as a constant inside the function. also we are trying to log decimalNumber, but thats the input, or inside the function the new variable which would result in a scope issue, since this does not exist outside of the function

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

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

return percentage;
}

console.log(decimalNumber);

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

// =============> Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared - so the same issue as the previous
//also after changing the decimalNumber to decimal inside the function: Uncaught ReferenceError ReferenceError: decimalNumber is not defined -->in console log we should ask for the function
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>
function convertToPercentage() {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}
console.log(convertToPercentage());
11 changes: 7 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

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

// =============> write your prediction of the error here
// =============> we should put num in the input of the function, instead of 3

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

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

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

//3 is unexpected. when we define a function we want to give it parameter(s), not a specific number. it is when the function is called that we want to give it specific values to pass in.
// Finally, correct the code to fix the problem

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

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

11 changes: 9 additions & 2 deletions Sprint-2/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
// =============> the function is not returning anything, it just logs to the console.

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:
// 320
// The result of multiplying 10 and 32 is undefined
// it prints the result of 10*32 since we called the function in our last console log, then it prints the sentence, but the sentence would include the return value of the function, but that is undefined, as the function did not return any value.

// 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)}`);
9 changes: 7 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> return is not followed by anything (directly), and a+b is after the ";" after return. in a function nothing gets executed after return

function sum(a, b) {
return;
Expand All @@ -8,6 +8,11 @@ function sum(a, b) {

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// =============> return value is undefined, so in the console log sentence the result (return value) is substituted with "undefined" instead of the actual 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)}`);
18 changes: 15 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> we did not give the function a parameter, and we are returning the last digit of 103 (num) instead of all the different numbers passed into this function

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

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -15,10 +15,22 @@ 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
// =============> because we did not define a parameter in the function to be used, instead we are using a variable that is declared to have a certain value (103) so the function will always perform the calculation on this variable
// 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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
7 changes: 5 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,8 @@
// 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
return (weight / height ** 2).toFixed(1);
}

console.log(calculateBMI(105, 1.67)); //37.6
7 changes: 7 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,10 @@
// 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 convertUpperSnakeCase(sentence) {
return sentence.replaceAll(" ", "_").toUpperCase();
}

console.log(convertUpperSnakeCase("hello world")); //HELLO_WORLD
console.log(convertUpperSnakeCase("lord of the rings")); //LORD_OF_THE_RINGS
23 changes: 23 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,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 convertPenceToPounds(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(convertPenceToPounds("2222p"));
console.log(convertPenceToPounds("22222p"));
console.log(convertPenceToPounds("23p"));
console.log(convertPenceToPounds("223p"));
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 @@ -15,24 +15,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
// =============> 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 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: the pad() function pads single digit numbers to 2 digits, to display times. since 60 seconds dont hold an hour, the first time unit is 0 (hour) then the second should be 1 since 60 sec is a min, and finally the last one refers to the seconds, after 60 there is 1 left to make it 61 secs.

// 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 since we pad the value (1) with a 0 to make it two digits.
Loading