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
40 changes: 37 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...
// =============> write your prediction here
// =============> My prediction is the code will not work properly
//due to "str" being a parameter on the first line, then it being declared
//again on the second line as a variable, essentially creating two declarations
// under one name, in the same scope.

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

// =============> write your explanation here
// =============> write your new code here
// =============> Uncaught SyntaxError: Identifier 'str' has already been declared - VM779:2
//Above is the error message i got, when i ran the code in my chrome console. My prediction
// was correct, 'str' has been declared twice, and also the error is on line 2.

// =============> I shall erase 'let' keyword and see what happens.

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

//function works, called on it by adding

capitalise("cyf");

//at the end.

















29 changes: 26 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> just like in the previous exercise, you can't have two variables
//with the same name in one scope. Second error is you can't call on "decimalNumber"
//from outside the function. Since that variable only lives inside the function.

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

function convertToPercentage(decimalNumber) {
//a parameter (decimalNumber) is created
const decimalNumber = 0.5;
//this is where the first error appears, as now there are two clashing variables
//under the same name. Code stops working.
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// =============> my prediction was correct.

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

//removed the clashing variable and placed the function call inside the
//console log.








11 changes: 9 additions & 2 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@

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

// =============> write your prediction of the error here
// =============> it won't work due to num variable not being set, also parameter can't be a number.

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

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

Uncaught SyntaxError: Unexpected number VM367:1

// =============> explain this error message here
//There is a syntax error on line 1 ie 3 acting as a parameter is wrong

// Finally, correct the code to fix the problem

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

console.log(square(3));

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

// =============> write your prediction here
// =============> this code needs a return function to insert 320 into the string

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
// =============> this code creates a function called multiply on line 5, line 6 is ignored for now because it's part of the function definition.
//Then JS read the code further from top to bottom until line 9, where it sees the values attached to the parameters and runs the function on 5 now, with new values.
//lines 6 then becomes console.log(320). Function ends here.

//

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

/*

this exercise is about understanding the differemce between console and return , in essense.
With return, the value can be inserted into the template literal.
With console.log, the value is only displayed and then discarded.

*/
13 changes: 10 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here

// =============> due to the semicolon on line 5 , the function will not progress further. In the console line template literal becomes
//`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)}`);

// =============> write your explanation here
// =============> my prediction was correct, i have removed the semicolon and put the expression on line 6 with the return function on line 5.

// 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)}`);
28 changes: 24 additions & 4 deletions Sprint-2/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
// =============> there is no paramater for getLastDigit to work with but a fixed value of 103 outside the function, so the console will show 3 everytime.

const num = 103;

Expand All @@ -14,11 +14,31 @@ 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
// =============>
VM447:7 The last digit of 42 is 3
VM447:8 The last digit of 105 is 3
VM447:9 The last digit of 806 is 3

undefined

// Explain why the output is the way it is
// =============> write your explanation here
// =============> because when the console.log calls for the function there are no parameters in it, so 42 gets ignored.
//However 103 is used because it is part of the return function under num keyword. Therefore causing the error for all the console lines.


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

// Now, with getLastDigit having a num variable, function will be able to assign different digits when console calls on it.


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

console.log(calculateBMI(80, 1.76));

25.8
//(I am officially overweight, :(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BMI has lots of limitations as a measuring system, so don't worry about it too much :)

9 changes: 9 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,12 @@
// 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) {
const upperCaseString = str.toUpperCase();
const snakeCaseString = upperCaseString.replaceAll(" ", "_");

return snakeCaseString;
}

console.log(toUpperSnakeCase("hello there"));
25 changes: 25 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,28 @@
// 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("666p"));
console.log(toPounds("69p"));
console.log(toPounds("13p"));
console.log(toPounds("1000p"));
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 @@ -21,18 +21,20 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> Pad's called three times, per hours, minutes and seconds each.

// 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
// =============> Answer is zero, due to 61 seconds being less than an hour.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> The function returns 00, after running twice.

// 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
// =============> When it runs for the last time it's calculating remaining seconds, with value of 61, after dedicting minutues the answer
//is 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
// =============> Answer is 01, because numString convert 1 to "1", then loop runs again, now it's "01", because of "while (numString.length < 2) {
// numString = "0" + numString;}" , then the loop stops.
61 changes: 61 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,64 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// i am runnign the following tests, since morning and afternoon times were done already,
// noon, different minutes.
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
/*
Since we know what the bug is, i shall handle it as a special case, and add the following code.

if (hours === 12) {
return `${time} pm`;
}

Tested it again and it works now.
*/
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}

const currentOutput3 = formatAs12HourClock("12:00");
const targetOutput3 = "12:00 pm";
console.assert(currentOutput3 === targetOutput3);

console.log(formatAs12HourClock("12:00"));

//i have run this code in dev tools , the asnwer was 12:00am , bug found.



function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3);

if (hours === 12) {
return `${time} pm`;
}

if (hours > 12) {
return `${hours - 12}:${minutes} pm`;
}
return `${time} am`;
}

const currentOutput3 = formatAs12HourClock("12:00");
const targetOutput3 = "12:00 pm";
console.assert(currentOutput3 === targetOutput3);

console.log(formatAs12HourClock("15:29"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Now that you have found the bugs, do you know what would be needed to fix them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for reviewing my code. I have fixed both bugs. Cheers.

//Answer was 3:00pm, bug found.

//PS: fixed the bug by adding a seperate variable for minutes. Tested it again and it works now.








Loading