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
1 change: 1 addition & 0 deletions Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ let count = 0;
count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe whaWt line 3 is doing, in particular focus on what = is doing
// Describe what line 3 is doing, in particular focus on what = is doing
4 changes: 2 additions & 2 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
penceString.length - 1,
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
paddedPenceNumberString.length - 2,
);

const pence = paddedPenceNumberString
Expand Down
17 changes: 13 additions & 4 deletions Sprint-3/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

// =============> We will have syntax error because string is the parameter of the function and the next line,it is declared wit let
//so it is declared two times in one part
// call the function capitalise with a string input

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

// =============> write your explanation here
// =============> write your new code here
// =============> write your explanation here SyntaxError: Identifier 'str' has already been declared
// =============> write your new code here.
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
17 changes: 13 additions & 4 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...
// Predict and explain first...:It doesn't run.We have a syntaxError.

// Why will an error occur when this program runs?
// =============> write your prediction here
// Why will an error occur when this program runs?because decimalNumber is declared.
// as a parameter and then declared again using const.

// =============> write your prediction here:We will have a SyntaxError because decimalNumber is a parameter of the function, but it is declared again using const inside the function.

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

Expand All @@ -14,7 +16,14 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> write your explanation here:decimalNumber is inside the function, so we can not use it outside the function.

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

console.log(convertToPercentage(0.5));
13 changes: 9 additions & 4 deletions Sprint-3/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:We will have a SyntaxError because we need a valid function parameter.

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

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

// =============> explain this error message here
// =============> explain this error message here:function square(3) {
//3 is a number, not a valid variable. Program expects a parameter name there.

// Finally, correct the code to fix the problem

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

console.log(square(3));

16 changes: 12 additions & 4 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

// =============> write your prediction here
// =============> write your prediction here:
//The code will run, but the output won't be what we expect because the multiply function logs the result instead of returning it.

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:console.log(a * b);
//The result is 320, but the function doesn't return a value. It shows us undefined because we don't have return in the function.
//320
//The result of multiplying 10 and 32 is undefined
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> 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)}`);
14 changes: 12 additions & 2 deletions Sprint-3/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
// =============> write your prediction here:This is the error:The sum of 10 and 32 is undefined.
// The code will run without a syntax error, but it will return undefined instead of 42.

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

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

// =============> write your explanation here
// =============> write your explanation here:
//The problem is that return; immediately ends the function://return;
//a + b;

//if we write anything after return(next line)is unreachable, so a + b doesn't run. The function therefore returns undefined.
// 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)}`);
25 changes: 21 additions & 4 deletions Sprint-3/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
// =============> Write your prediction here:I predict the output will be 3 every time, because the function uses the global constant num = 103 instead of using the numbers passed into getLastDigit().

const num = 103;

Expand All @@ -14,11 +14,28 @@ 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
// =============> 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
// =============> write your explanation here:there is no parameter in the function.
//So when we call the function:
//getLastDigit(42)
//the 42 is passed to the function, but the function has no parameter to receive it.
//Instead, it uses the global variable (const num = 103)
//so every call converts 103 to a string and gets its last digit: 3.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here:
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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
4 changes: 3 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
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) {
// 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
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 toUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
24 changes: 24 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,27 @@
// 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("250p"));
console.log(toPounds("99p"));
console.log(toPounds("5p"));
33 changes: 28 additions & 5 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,41 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> write your answer here: three calls to pad() in the return statement:
//return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;

// 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:
//For formatTimeDisplay(61):
//remainingSeconds = 61 % 60 = 1
//totalMinutes = (61 - 1) / 60 → 1
//remainingMinutes = 1 % 60 → 1
//totalHours = (1 - 1) / 60 → 0
//num = 1

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> write your answer here:num is 0.
//let numString = num.toString(); gives us 0

// This loop adds a "0" because the string has only one character:
//numString = "0" + "0";
//return value is:
//"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:
//The last call is: pad(remainingSeconds)

//For 61 seconds, remainingSeconds is 61 % 60 = 1:
//so 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
// =============> write your answer here:
//num is 1, so:
//num.toString() gives "1".

// and this loop adds a 0 to the beginning: "01"

//So the final result of formatTimeDisplay(61) is: 00:01:01
Loading