Skip to content

LONDON_JAN25 | KHALIL ALI | STRUCTURING_AND_TESTING_DATA | SPRINT 2 | WEEK 5 #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> str was declared twice, once as a parameter and once as a variable.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +9,13 @@ function capitalise(str) {
return str;
}

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


// =============> SyntaxError: Identifier 'str' has already been declared
// =============> this error because the variable str was declared twice.
// =============> write your new code here
function capitalise(str) {
return str =`${str[0].toUpperCase()}${str.slice(1)}`;
Copy link

Choose a reason for hiding this comment

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

Why not just return the constructed string directly? Why assign the string to a variable first?

}

console.log(capitalise('hi there'));
25 changes: 17 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> decimalNumber is declared twice, once as a parameter and once as a variable.
// =============> console.log(decimalNumber); should be console.log(convertToPercentage(0.5));

// 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
// =============> SyntaxError: Identifier 'decimalNumber' has already been declared
// =============> this error because the variable decimalNumber was declared twice.

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

return percentage;
}
const decimalNumber = 0.5;
console.log(convertToPercentage(decimalNumber));
12 changes: 8 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,22 @@

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

// =============> write your prediction of the error here
// =============> right function declare should have a variable parameter instead of number

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 a number instead of a variable was declared as a parameter

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
let num = 3;
console.log(square(num))
20 changes: 14 additions & 6 deletions Sprint-2/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 function multiply does not return a value, so the console.log will print 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

// the output is:
// 320
// The result of multiplying 10 and 32 is undefined
// there is no return in multiply function so it will return 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)}`);
11 changes: 10 additions & 1 deletion 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
// =============> write your prediction here there is no thing after return in the function sum so it will return undefined.

function sum(a, b) {
return;
Expand All @@ -9,5 +9,14 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// the output is:
// The sum of 10 and 32 is undefined
// there is no return in sum function so it will return 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)}`);
30 changes: 22 additions & 8 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 the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here the function getLastDigit does not have a parameter

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
// because the function getLastDigit does not have a parameter,
// so every time returns the last digit of the num(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
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)}`);
5 changes: 4 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,7 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
let BMI = (weight/(height*height)).toFixed(1);
return Number(BMI);
}
console.log(`my BMI is ${calculateBMI(100, 1.78)}`);
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 snakeCase (str){
return str.toUpperCase().replace(/ /g, `_`);
}
console.log(snakeCase("my name is khalil ali"));
30 changes: 30 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Copy link

Choose a reason for hiding this comment

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

Can you properly indent the code in this file?
VSCode has a built-in functionality to auto-indent code.

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,33 @@
// 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
const penceString = "399p";
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("100p"))
console.log(toPounds("1000p"))
// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds

// You need to do a step-by-step breakdown of each line in this program
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
// 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 because the remaining seconds is 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 "01" because the pad function adds zero to the start of the number
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if(hours===0){
return `12:00 am`
}
if(hours===12){
return `12:00 pm`
}
Comment on lines +7 to +12
Copy link

Choose a reason for hiding this comment

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

The minute parts are not always "00".

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
Expand All @@ -23,3 +29,7 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.log(formatAs12HourClock("00:00"))
console.log(formatAs12HourClock("06:00"))
console.log(formatAs12HourClock("12:00"))
console.log(formatAs12HourClock("23:00"))