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
26 changes: 22 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
// Predict and explain first...
// =============> write your prediction here
//
// We'll get syntax error with variable declaration. Function already have
// the variable str as a parameter. Then we try to declare another variable with
// the same name in the function body.

// 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;
// }
//
// capitalise("module structuring and testing data");

// =============> write your explanation here
//
// As predicted we cath error 'SyntaxError: Identifier 'str' has already been declared'
//
// =============> write your new code here

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

console.log(capitalise("module structuring and testing data"));


34 changes: 27 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

//
// We'll get a syntax error with redeclaration of the exist variable. The variable
// decimalNumber is already declared as a parameter of the function, but then we
// try to declare and initialize new constant with the same name. Other problem
// is that we try to use in the console.log() function invocation variable declared
// inside the function and don't accessible from global scope.
//
// 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
//
// As predicted we got an error 'SyntaxError: Identifier 'decimalNumber' has
// already been declared'. But solution depends of logic of the function.
//
// Finally, correct the code to fix the problem
// =============> 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);

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

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(decimalNumber));
26 changes: 20 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
//
// We have function declaration errors:
// 1. Instead of a parameter there is an numeric value in the function signature
// 2. We try to use undeclared variable num in the body of the function.

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

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

//
// SyntaxError: Unexpected number
//
// =============> explain this error message here

//
// As predicted error is a numeric literal instead of the function parameter.
//
// Finally, correct the code to fix the problem

//
// We need to change the number 3 with name of variable is used in function
//
// =============> write your new code here

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

console.log(square(3));
23 changes: 19 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

// =============> write your prediction here

function multiply(a, b) {
console.log(a * b);
}
// In function multiply first will be evaluated the multiply expression in parentheses.
// Then the received value will be printed in the console. So as result of running code
// we'll get result output:
// 320
// The result of multiplying 10 and 32 is undefined

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// 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

// The multiply(a, b) function doesn't have a return value. So every time we run
// it we'll get undefined as a return 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)}`);
24 changes: 19 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here

function sum(a, b) {
return;
a + b;
}
// As the result of function's work we'll get undefined value. So th code running
// result will look like:
// The sum of 10 and 32 is undefined

// 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

// It happens because function's return statement stands before summing expression
// and return as function's work result undefined value.

// 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)}`);
38 changes: 31 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,46 @@
// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
// The output will looks lke:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

function getLastDigit() {
return num.toString().slice(-1);
}
// const num = 103;

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

// 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 given function doesn't have arguments and every time operate with variable
// num that is declared as global and is initialized with value 103.

// Finally, correct the code to fix the problem
// =============> 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
2 changes: 1 addition & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
// 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 (weight / Math.pow(height, 2)).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 toUpperSnakeCase(str) {
return str.toUpperCase().replaceAll(' ', '_');
}
83 changes: 83 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,86 @@
// 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 paddedPenceNumberString = penceString.substring(0, penceString.length - 1).padStart(3, '0');

return `£${paddedPenceNumberString.substring(0, paddedPenceNumberString.length -2)}.${
paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, '0')}`;
}

let currentOutput = toPounds("0p");
let targetOutput = '£0.00';
console.assert(currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
)

currentOutput = toPounds("3p");
targetOutput = "£0.03";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("20p");
targetOutput = "£0.20";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("99p");
targetOutput = "£0.99";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("100p");
targetOutput = "£1.00";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("101p");
targetOutput = "£1.01";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("120p");
targetOutput = "£1.20";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("543p");
targetOutput = "£5.43";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("999p");
targetOutput = "£9.99";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("1000p");
targetOutput = "£10.00";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);

currentOutput = toPounds("1001p");
targetOutput = "£10.01";
console.assert(
currentOutput === targetOutput,
`Current output: '${currentOutput}', target output '${targetOutput}'`
);
16 changes: 16 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,32 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// The function pad(num) will be called 3 times in line 11.


// Call formatTimeDisplay with an input of 61, now answer the following:

formatTimeDisplay(61);

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here

// num = 1;


// c) What is the return value of pad is called for the first time?
// =============> write your answer here

// return value = '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

// num = 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

// return value == '01'
Loading