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
13 changes: 9 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// =============> my explanation: I see here declaration of str variable, which already exist as an argument. It will cause an error.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

//===============>SyntaxError: Identifier 'str' has already been declared, its occured while execution of code.
/*
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
*/
// =============> write your explanation here. My explanation: We have to change the name of variable and return it in order to fix an error.
// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("some string"));
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here. My prediction: Value decimalNumber is a parameter to the convertToPercentage. We don't need to declare it at line 10.
// =============> Also at line 16, we have to call function coverToPercentage with an argument.

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

function convertToPercentage(decimalNumber) {
/*function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
*/
// =============> write your explanation here. My explanation: We need to modify this function. I'm going to remove line where decimalNumber is declared and execute the function properly.

// Finally, correct the code to fix the problem
// =============> write your new code here

function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}

console.log(convertToPercentage(0.1));
15 changes: 10 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

// 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 My prediction: There is an error with parameter.
/*
function square(3) {
return num * num;
}
console.log(square)
*/
// =============> write the error message here: SyntaxError: Unexpected number

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

// =============> explain this error message here
// =============> explain this error message here: An error because of invalid parameter.

// Finally, correct the code to fix the problem

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


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

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

// =============> write your prediction here: This function return nothing.
/*
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: We need to modify this function.
// =============> Create a new variable where we have to do the following calculation, after that return this new variable.

// 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)}`);
15 changes: 10 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here

// =============> write your prediction here: wrong calculation and return nothing.
/*
function sum(a, b) {
return;
a + b;
}

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

// =============> write your explanation here
*/
// =============> write your explanation here: We need to create a variable where we have to store our calculation and return it.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code
function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
19 changes: 14 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here

// =============> Write your prediction here: Function getLastDigit return num.
/*
const num = 103;

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
// =============> write the output here: output is last digit of variable num.
// Explain why the output is the way it is
// =============> write your explanation here
// =============> write your explanation here: Because num is global and function's parameter is absent .
// 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
4 changes: 3 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,6 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return weight/Math.pow(height,2);
}
console.log(calculateBMI(70, 1.73));
6 changes: 6 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,9 @@
// 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){
let textUpper = text.toUpperCase();
return textUpper.replace(" ", "_");
}
console.log(upperSnakeCase("hello here"));
21 changes: 21 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
/*
// In Sprint-1, there is a program written in interpret/to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// 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){
let penceStringWithoutTrailingP;
if (penceString.charAt(penceString.length - 1) == "p"){
penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1);
} else {
penceStringWithoutTrailingP = penceString;
}
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);

const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

console.log(`£${pounds}.${pence}`);
}
toPounds("200");
toPounds("200p");
toPounds("20");
toPounds("2");
15 changes: 10 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,23 @@ function formatTimeDisplay(seconds) {
// 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
// =============> 61%60=1/61-1/60=1/60%60=0, the value assigned to num when pad called for the first time is 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: remainingSeconds 61%60=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 of .padStart(2, "0")
console.log(pad(0));
console.log(pad(1));
console.log(pad(123));
console.log(formatTimeDisplay(61));

23 changes: 23 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,26 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

/* test */

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

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

8 changes: 8 additions & 0 deletions time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function formatAs12HourClock() {}

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