Skip to content
Open
21 changes: 14 additions & 7 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
// =============> I predict it will throw a syntaxerror because the parameter 'str' is already declare in the definition,
//and line 9 try to declare another variable with the same name so the error will be identifier 'str'has already been declared .


// 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)}`;
return str;
}
// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }
// capitalise("ahmadhmedan");

// =============> write your explanation here
// =============> write your new code here
// =============> As I predict it will throw a SyntaxError: Identifier 'str' has already been declared
//because in JavaScript we can not redeclare the same variable in the same scope.
function capitalise(str){
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("ahmadHmedan"));
26 changes: 18 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

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

// my prediction it will throw a syntaxerror because the parameter decimalNumber is already declare in the definition.
//and line 11 try to declare another variable with the same name so the error will be identifier 'str'has already been declared .
//
// Try playing computer with the example to work out what is going on

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

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here
// =============> As I predicted, SyntaxError: Identifier 'decimalNumber' has already been declared

// 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));
19 changes: 11 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

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

// =============> write your prediction of the error here
// =============> // It will throw a syntaxerror because in the function definition we sit the parameter name not the value

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

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

// =============> explain this error message here
// =============> As I predicted we must set the parameter name in the definition

// Finally, correct the code to fix the problem

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

// =============>
function square(num) {
return num * num;
}

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

// =============> write your prediction here
// =============> it will throw a syntaxerror because I can not use nested console.log.

function multiply(a, b) {
console.log(a * b);
}
// function multiply(a, b) {
// console.log(a * b);
// }

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// =============> The result of multiplying 10 and 32 is undefined undefined because If I want to keep the value with me after the function finishes, I must use return.

// 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)}`);
19 changes: 12 additions & 7 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
// =============> It will not return the value.

function sum(a, b) {
return;
a + b;
}
// 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
// =============> any think we do after return will not coding.
// 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)}`);
37 changes: 26 additions & 11 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> first it will convert the number to string then it will slice the last character that function
//then will print
//The last digit of 42 os 2
//The last digit of 105 os 5
//The last digit of 806 os 6

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// 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 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
// =============> 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
// =============> write your explanation here
// =============> Because the function always takes the global variable witch is 103
//the output will be the same witch 3.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code her


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
6 changes: 4 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,7 @@
// 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 (weight/(height*height)).toFixed(1);

}
console.log(calculateBMI(70,1.73));
23 changes: 23 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,26 @@
// 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 ConvertToUpperSnakeCase(s1)
// {
// let result = "";
// for(let i=0;i<s1.length;i++)
// {

// if(s1[i]===" ")
// {
// result+=s1[i]="_";
// }
// else
// result+=s1[i];
// }
// let upperSnakeCase=result.toUpperCase();
// return upperSnakeCase;
// }
// console.log(ConvertToUpperSnakeCase("Ahmad Hmedan"));

function convertToUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}
console.log(convertToUpperSnakeCase("Ahmad Hmedan"));
9 changes: 9 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,12 @@
// 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 toPound(penceString) {
const penceWithoutP = penceString.substring(0, penceString.length - 1);
const paddedPence = penceWithoutP.padStart(3, "0");
const pound = paddedPence.substring(0, paddedPence.length - 2);
const Pence = paddedPence.substring(paddedPence.length - 2);
return `£${pound}.${Pence}`;
}
console.log(toPound("100p"));
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 @@ -3,32 +3,33 @@ function pad(num) {
}

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

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
// =============> Three time

// 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
// =============> num =0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> return value='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
// =============> num=1 because it will take the value of remainingseconda which is 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
// =============> return value ='01' it will take number 1 then will convert to sting then it will add 0 to the first because it is only one character .
Loading