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

// The Error will occur because we are trying to create a variable with same name in same scope.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

let str = 'hello';
console.log(capitalise(str));
// =============> write your explanation here
// =============> write your new code here
//By capitilise(str)-> we have created a function with parameter str.
// Inside the function, `str` is already declared as a local variable because it is a parameter.
// We can't declare another variable with the same name using `let` in the same scope.
// It was giving an error because we were trying to declare `str` again using `let`.
// I corrected it by removing `let` because we don't need to create another variable with the same name; we just have to assign a new value to the existing `str`.
// Outside the function, I created another variable with the same name, `str`. We can do this because it is in a different scope.
32 changes: 30 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// There're two errors.
// First Error will be, the function having a parameter decimalNumber, and we are trying to declare another variable with same which we can't be able to do.
// And decimalNumber is a local variable , we cannot use it outside of the function/scope.


// 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);
console.log(decimalNumber);*/

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

return percentage;
}
const decimalNumber= 0.9;
console.log(decimalNumber);
console.log(convertToPercentage(decimalNumber));
// =============> write your explanation here

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

// By function convertToPercentage(decimalNumber) -> we have created a function with the parameter decimalNumber.
// Inside the function, `decimalNumber` is already declared as a local variable because it is a parameter.
// We can't declare another variable with the same name using `const` in the same scope.
// we need to assign a value to the variable.
// Outside of the function we are trying to print the value of decimalNumber but we haven't declare the variable nor assign a value.
// And decimalNumber is a new variable as its outside of the scope now, so if we want we can create a variable with the name used before in a function
// because its in different scope.
// I have created a const variable and assigned a value to it.
// And lastly, I have called function and passed decimalNumber as an argument and print the value what's function is returning.
// The percentage is calculated by decimalNumber = 0.5 not 0.9
// As we passed decimalNumber as an argument with the value of 0.9 but inside the function we are assigning a value to the
// decimalNumber, its value will be overwritten with parameter's local value and percentage will be calculated with decimalNumber=0.5
// If we don't assign a value inside the function then percentage will be calculated on the basis of the argument we passed,
// though outside of const decimalNumber will be unchanged and remained 0.9.
16 changes: 11 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
//We cannot use number as a parameter. It should be any variable.

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

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

//The error message is, 'Unexpected number'.
// =============> explain this error message here

// it says unexpected in terms, javaScript doesn't expect number/value as a parameter.
// Finally, correct the code to fix the problem

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


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

const result = square(3);
console.log(result);
15 changes: 11 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

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

function multiply(a, b) {
// In this example function is being called but as there's no return statement so it will return undefined.
// and a *b multiplication. and the line on line 10
/*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

// I fixed it by adding a return statement and create another variable outside of the function that stores,
//value of function.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return (a*b);
}
const result = multiply(10,32);
console.log(`The result of multiplying 10 and 32 is ${result}`);
17 changes: 14 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here

function sum(a, b) {
//It will give an error as we have to mention what are we returning, by simply putting the return statement doesn't automatically
//return anything, except undefined, and then we are doing an addition of two number but we haven't return the result of it,
//when the function will be called in line 11, it will print undefined with the sentence mentioned in literals.
/*function sum(a, b) {
return;
a + b;
}

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

// =============> write your explanation here
//I firstly fixed the return statement.
//outside of the function I create another variable that stores function value.

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

function sum(a, b) {
return a+b;
}
const result = sum(10,32);
console.log(`The sum of 10 and 32 is ${result}`);
21 changes: 18 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

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

const num = 103;
// we are using the constant variable num, and it is declared as a global variable. The function is declared with no
// parameter, but, while calling a function we're passing an argument. It can't use that argument as function is declare
// with no parameter so it will use the global variable whenever the function is being called.
/*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)}`);
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
// it's returning 3 for all three digits which is wrong for that digits
// Explain why the output is the way it is
// =============> write your explanation here
//This is happening because function is declare with no parameters as I predict and my prediction is totally right as javaScript
//doesn't allow to use argument when there's no parameters.
// Finally, correct the code to fix the problem
// =============> write your new code here

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
// it wasn't working because we are passing argument without declaring function with a parameter and function was using
//global variable whenever the function is being called, so it was returning the last digit of that value stored in global variable.
10 changes: 9 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,12 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const squareHeight = height * height;
const div = weight / height;
const result = div.toFixed(1);
return result;
}
const height = 2.3;
const weight = 64;
const result = calculateBMI(weight, height);
console.log(result);
14 changes: 14 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,17 @@
// 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 SnakeUpperCase(str){
var result = str.toUpperCase();
let strLength = result.length;
for (let i=0; i<strLength; i++){
if(result.charAt(i)==" "){
newstr = result.replaceAll(" ", "_");
}
}
return newstr;
}
let sentence = "hello world i am maryam";
let result = SnakeUpperCase(sentence);
console.log(result);
18 changes: 18 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,21 @@
// 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 ConversionPenceToPounds(penceString){
const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2);
paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2);
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
const result = `£${pounds}.${pence}`;
return result;
}
const pencestring1 = ConversionPenceToPounds("663p");
const pencestring2 = ConversionPenceToPounds("98p");
const pencestring3 = ConversionPenceToPounds("986p");
console.log(pencestring1);
console.log(pencestring2);
console.log(pencestring3);
32 changes: 19 additions & 13 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
function pad(num) {
let numString = num.toString();
while (numString.length < 2) {
function pad(num) { //1)num=0, 2)num=0 3) num=1
let numString = num.toString(); // (last time) numString = "1"
while (numString.length < 2) { //"01" it will return 01
numString = "0" + numString;
}
return numString;
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60; //61%60 = 1
const totalMinutes = (seconds - remainingSeconds) / 60; //1-1=0
const remainingMinutes = totalMinutes % 60; //0
const totalHours = (totalMinutes - remainingMinutes) / 60; // 0

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
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
Expand All @@ -22,17 +24,21 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

//3 in console.log
// 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)num=0, 2)num=0 3) num=1, when num is called for last time that is when pad(remainingSeconds),
//here remainingseconds value = 1 so num is assigned with value of 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
//when pad called for the last time, the value of num is 1
//it will convert into string by toString method "1" as its less than 2, it satisfies the while loop condition and,
// will concatenate with "0" the final value of variable numString would be "01", and will return "01".
28 changes: 28 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,31 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "00:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

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

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

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

Loading