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
11 changes: 8 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

// My prediction is that the function will throw an error because the variable which also the parameter is redeclared again inside the function.
// 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,10 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// =============> write your explanation here: to explain, The parameter str already exists in that function's scope,
// so trying to `let str = ...` again is redeclaring the same name in the same scope, and JS won't allow it.
// =============> write your new code here: function capitalise(str) {
// let capitaliseFristLatter = `${str[0].toUpperCase()}${str.slice(1)}`;

// return capitaliseFristLatter;
// }
14 changes: 11 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

// My prediction is that the variable `decimalNumber` is already declared and also the console.log can only log the variable decimalNumber.
// Try playing computer with the example to work out what is going on

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

console.log(decimalNumber);

// =============> write your explanation here
// =============> write your explanation here: the variable was already declared in the parameter scope and it won't redeclare inside the function again
// and also the console.log was only working for the logging of the variable `decimalNumber` not the function.

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

// return percentage;
// }

// console.log(convertToPercentage(decimalNumber));
18 changes: 10 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

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

// 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: it will throw an error because in the parameter(placeholder)
// there is constant value which js does not recognize.

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

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

// =============> explain this error message here
// =============> explain this error message here: syntax error, meaning it fails before the function even gets defined.
// JS expects a parameter slot to contain a name (identifier) and gets confused
// when it sees a raw number sitting there instead.

// Finally, correct the code to fix the problem

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


// =============> write your new code here: function square(num) {
// return num * num;
// }
14 changes: 11 additions & 3 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: My prediction is that in this function there is two console.log
// the result of this two log will print one with the value only 320 and
// the other one with the sentences of the log not the value.

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: because of the log inside the function the outside function can't access the result so the log will print
// one with the value only and second console.log will print the sentences with explanation of the result and
// undefined value because the the function doesn't access the operation.

// 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)}`);
12 changes: 9 additions & 3 deletions 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: the function have nothing to return so it will log the sentences with undefined value.

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

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

// =============> write your explanation here
// =============> write your explanation here: firstly the process start accepting that it is a function and takes
// the parameter to hold the place then it go through the function but by the 5th line
// the function return and the value will come out undefined,which means java script doesn't
// have step to continue so it is returning with out a value.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here: function sum(a, b) {
// return a + b;
//
// }
12 changes: 8 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
// =============> Write your prediction here: the code is already declared a variable that can not be changed so the consol.log always be the same 3.

const num = 103;

Expand All @@ -14,11 +14,15 @@ 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: 3
// Explain why the output is the way it is
// =============> write your explanation here
// =============> write your explanation here: the code is already declared a variable that can not be changed in the
// first place so when ever the process try to access another console.log it will take the same value as before
// so the consol.log always be the same.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here: function getLastDigit(num) {
// return num.toString().slice(-1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does -1 represent when passing into slice?

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.

  1. when using slice if we apple -1 to the slice it represent that it is starting from the last index but not including 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
}
const squaredHeight = height * height;
const originalBMI = (weight / squaredHeight).toString();
let dotIndex = originalBMI.indexOf(".");
let bmiWithOneDecimal = originalBMI.slice(0, dotIndex + 2);
return bmiWithOneDecimal;
}
console.log(`The calculated BMI is ${calculateBMI(78.4, 1.71)}`);
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 toUpperSnakeCase(str) {
const toCapitalized = str.toUpperCase().replace(/ /g, "_");
return toCapitalized;
}
19 changes: 19 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,22 @@
// 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}`;
}
13 changes: 7 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));
// You will need to play computer with this example - use the Python Visualiser 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
// =============> 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:it is 0, because totalHours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here:it is 00, because the function pad will add another 0 if it is less than 2 index.

// 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:it is 1, because the remainingSeconds value will become 1 after it get the remainder of the seconds.

// 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:it will be 01,because after going through the function pad it will gain 0 from the front because it is less
// than 2 index so the return will be 01.
29 changes: 24 additions & 5 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,41 @@

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

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

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

const currentOutput3 = formatAs12HourClock("00:27");
const targetOutput3 = "12:27 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);
Loading