Skip to content

LONDON | MAY_25 | EMILIANO URUENA | SPRINT2 #513

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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
13 changes: 12 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

/*
The variable 'str' must be defined before it be used in a function. As a result, an error of Identifier will occur.
*/
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -10,4 +12,13 @@ function capitalise(str) {
}

// =============> write your explanation here
// The error is occurring because the parameter 'str' is being redeclared with 'let' inside the function.
// This causes a conflict and results in an error. To fix this, we can simply remove the 'let' keyword
// when reassigning the value to 'str'.

// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
//console.log(capitalise("Texto"))
17 changes: 13 additions & 4 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...

/*
The console is returning decimalNumber, which is the original number instead of the result of the function.
The variable decimalNumber was declared in the function parameters.
*/
// Why will an error occur when this program runs?
// =============> write your prediction here

// An error of variable, because the variable decimalNumber must was declared inside of the 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}%`;

return percentage;
}

console.log(decimalNumber);

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

// The variable decimalNumber was declared inside the function. Also, the console.log was trying to call the variable decimalNumber.
// Finally, correct the code to fix the problem
// =============> write your new code here

function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
console.log(convertToPercentage(0.5));
21 changes: 15 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// Predict and explain first BEFORE you run any code...

// this function should square any number but instead we're going to get an error
/*
The function suppose to elevate a number to the square. However, the parameter of the function,
the declaration of the variable 'num' and the execution is wrong.
*/

// =============> write your prediction of the error here
// Parameters error line 11, 3.

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

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

/*
SyntaxError: Unexpected number
function square(3)
^
*/
// =============> explain this error message here

// The parameter of the function expects a name instead of a number.
// Finally, correct the code to fix the problem

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


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

// The function suppose to multiply two numbers. The execution of the function includes a console.log call instead of a return.
// =============> write your prediction here

// The console is going to present two different messages. No one with the expected result.
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 console shows the result of the multiplication 10 x 32 = 320, and the text message with the result 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 ad 32 is ${multiply(10,32)}`);
8 changes: 7 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...
// =============> write your prediction here

// This is a function to sum two numbers, 10 and 32, and show a message with the result.
// Will return undefined error because the 'return' of the function is empty.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +10,10 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The return of the execution code in the function must be the operation a + b
// 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)}`);
21 changes: 18 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...

// The function must show the last digit a number, starting with 103, that is to say 3.
// Then evaluate other numbers; 42 result 2, 105 result 5, and 806 result 6.
// Predict the output of the following code:
// =============> Write your prediction here

// The console messages will show undefined results.
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +16,24 @@ 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
// The function take the the number 103 from the variable num in the execution, getting the result 3.
// However, the parameter of the function was not stablish with the variable. As a result, the return never changed.
// =============> write your explanation here
// The parameter of the function was not stablish with the variable. As a result, the return never changed.
// 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 32 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
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
}
return (weight/(height*height)).toFixed(1);
//return Math.round((weight/(height*height))*10)/10; //This is other option to fix the number of decimals to 1.
}
console.log(`According your values of weight and height, your BMI is: ${calculateBMI(90,1.73)}`);
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

function upperSnakeCase(text){
return (text).toUpperCase().replaceAll(' ','_');
}
console.log(upperSnakeCase('code your future'));
17 changes: 17 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,20 @@
// 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(number){
if(number.toString().slice(-1) !== 'p'){
number = (number.toString() + 'p');
}
const penceString = number;
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.assert(toPounds('399')==='£3.99','Test fail with; 399');
console.assert(toPounds('399p')==='£3.99','Test fail with; 399p');
console.assert(toPounds(40)==='£0.40','Test fail with; 40');
console.assert(toPounds('1')==='£0.01','Test fail with; 1');
console.assert(toPounds('34947p')==='£349.47','Test fail with; 34947p');
12 changes: 6 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// You will need to play computer with this example - use the Python Visualizer 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
// =============> Three 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
// =============> 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 last time when pad is called is pad(remainingSeconds) the remainder of 61/60 = 1 calculated in line 6

// 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
// =============> The return value assigned is 01, the remainder was 1 and pad function fill blanks with 0 to get 2 chars.
38 changes: 27 additions & 11 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.

function formatAs12HourClock(time) {
if (!time.includes(':')){
time = time.slice(0,-2)+':' + time.slice(-2)
}
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = time.slice(3);
return `${hours - 12}:${minutes} pm`;
}else if (time.length < 5){
time = time.padStart(5,'0')
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
console.assert(currentOutput === targetOutput,`current output1: ${currentOutput}, target output: ${targetOutput}`);

const currentOutput2 = formatAs12HourClock("23:01");
const targetOutput2 = "11:01 pm";
console.assert(currentOutput2 === targetOutput2,`current output2: ${currentOutput2}, target output: ${targetOutput2}`);

const currentOutput3 = formatAs12HourClock("07:34");
const targetOutput3 = "07:34 am";
console.assert(currentOutput3 === targetOutput3,`current output3: ${currentOutput3}, target output: ${targetOutput3}`);

const currentOutput4 = formatAs12HourClock("1100");
const targetOutput4 = ("11:00 am");
console.assert(currentOutput4 === targetOutput4,`current output4: ${currentOutput4}, target output: ${targetOutput4}`);

const currentOutput5 = formatAs12HourClock("8:24");
const targetOutput5 = "08:24 am";
console.assert(currentOutput5 === targetOutput5,`current output5: ${currentOutput5}, target output: ${targetOutput5}`);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
const currentOutput6 = formatAs12HourClock("1:01");
const targetOutput6 = "01:01 am";
console.assert(currentOutput6 === targetOutput6,`current output6: ${currentOutput6}, target output: ${targetOutput6}`);