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-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,45 @@

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// The error is SyntaxError: Identifier 'str' has already been declared
//in line 9 and 10 str is declared as a parameter of capitalise and again when let is used to initalise it
//syntax error means something in the code violate javascript rules
//in this case str has been declared twice
//I had to search solutions for this case which I encountered several times.
//it looks like a common error

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

// =============> write your explanation here
// =============> write your new code here
//the solution would be one of these:
//the easiest solution would be to remove let and not to declare it again, since it's already declared inside a function as a parameter
//other solutions I will list them later

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

Suggested change
str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;

Hi I you've done a great work identifying the error. To be more precise,
The real problem here is that str is declared twice: once as the function parameter
once again with let str = ...

return str;
}
console.log(capitalise("hi"));

// Predict and explain first...
// =============> write your prediction here
// A function capitalise is caolled, and a variable called str is initialised
//the variable str value is assigned using the string methods toUpperCase and slice

//than a return function is called to show the str value
//I predict that the input str characters get all capitalsed becuase toUpperCase method
//it starts applying this from index 0 or first character and the slice method is telling it to leave every character afrter the index 1 as it is

//so where does it end?
//another method str.slice is used to extract part of the string and returning it
//here it has a start index only which is 1 and the end index omitted
//because the end index omitted it will start from index 1 to the end of the string

//I predict the value returned to be nothing because no value was assigned to str
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

// =============> write your explanation here
// =============> write your new code here
34 changes: 27 additions & 7 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
// Predict and explain first...

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

// =============> write your prediction here
//this has the same issue as the one I encountered in the previous example. decimalNumber is declared twice
//so I'm expecting a syntaxError, the identifier decimalNumber has been declared twice
// Try playing computer with the example to work out what is going on

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

// return percentage;
//}

//console.log(decimalNumber);

// =============> write your explanation here
//the solution is to make the variable declared only once as a parameter
// and than remove the declaration from line 11 by removing let and reassigning its value
// Finally, correct the code to fix the problem
// =============> write your new code here

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
console.log(convertToPercentage(0.8));
//ok I had to correct my solution because a reference error is caused by
//calling a parameter that only identified inside the function
//the solution would be to change line 18 to call the function and input
//input the value we want to convert because no point of declaring identifier twice
//what I notice by connicting both examples: the previous example the identifier was declared twice inside the function which caused a syntaxError
//in this example there is also declaring identifier that only identified inside a function , so the program doesn't recognise it which cause
//cause a reference error

// Finally, correct the code to fix the problem
// =============> write your new code here
23 changes: 16 additions & 7 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// 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
//ok, according to javaScript rules or convention for variables, we can't start with a number
//I'm expecting a syntaxErrror because its against javaScript rules

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

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

//SyntaxError: Unexpected number
//not bad for a prediction-_-
// =============> explain this error message here

//it means javaCript can't identify what this number represent
//it's not part of the built in rules
// Finally, correct the code to fix the problem

//we just change number 3 to a the word three and assign its value to num
// =============> write your new code here
function square(three) {
num = three;
return num * num;
}
console.log(square(4));


//so far this sprint is easy-_-
Empty file.
24 changes: 18 additions & 6 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// Predict and explain first...

// =============> write your prediction here
//inside the function console.log doesn't have the value of a and b parameters
//if I think like the computer than I will see a function declared with parameters a and b
//next line I will see a print function of the parameters a and b with operator *
//I'm not a computer but I think if I was , I will print a * b
//than I will call the multiply function again to show the message
//The result of multiplying 10 and 32 is 320
//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
//ok, I was werong in my prediction about console.log not using the parameters value outside the function, which I correctr my understanduing Headers.apply
//what happens is the result is printed twice, the solution would be to remove the console.log from inside the function and use return instead while keeping console.log function call or in line 15

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
return (a, b);
return (a* b);

I think this is still is still incorrect. It should be return a * b; to be able to perform the multiplication.

}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
28 changes: 22 additions & 6 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here
//ok, I predict a syntaxError: a and b are not identified, and I think this because
//because the use of return function, I haven't encountered a similar case but I think it has to do
//with what the return function do, it returns the values of the parameters so
//so when a and b are used there is an issue happining but I can't say what because I don't have the information
//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
//ok I see this error "The sum of 10 and 32 is undefined"
//I had to search what return do that cause this to be able to explain it correctly.
// this is my explaination before searching " I'm suspecting the error is caused by the return function returning the parameters values so when we use a + b; the program doesn't find what we are looking for because it was already returned"
//after searching I understand it caused becaused return means exiting the function , so line 10 will never be excuted

// Finally, correct the code to fix the problem
// =============> write your new code here
//the correction would be to include a + b inside return function so the value become identified
// ===========> write your new code here

function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
38 changes: 30 additions & 8 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@

// Predict the output of the following code:
// =============> Write your prediction here
//ok the issue is clear, const is used to initialise the variable num
//at the same time num supposed to take several inputs or change its inputs
//to get the last digit of each number. However, its declared before the function using const so its value can't be reassigned
//so I'm assuming its value will always be excuted in the calculation

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 num was declared as a constant value 103 , so the assigned value of num is 103

//so out output of the last digit will calculate num when the input is 103 only
//no matter how many numbers we include in the brackets, the excution of the function depend on the declared num value
//num is declared with const and has the value 103
//the solution is to declare num as a parameter inside the function
// 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);
}

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
//because the declared declared variable num is assigned a constant value 103.
//the solution is to delete the loine before the function and declare num as a parameter inside the function
13 changes: 12 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place

//function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
//}

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
return (weight / (height * height)).toFixed(1);
}

console.log(`Your Bmi is ${calculateBMI(70, 1.73)}`);

//why I used toFixed?
//Its a javaScript method used to format a number's digit based on my request
//it rounds the number and fill the digit with zeros if needed
//also the parenthises mean, it will return the result as a text or string
15 changes: 15 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@
// 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 capitalise_all_letters(str) {
//I used toUpperCase string method to capitalise all the string
//I used replace to replace first space " " in the string and replace it with "_"
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(
`The result is ${capitalise_all_letters("hello world we are here")}`,
);

// so far its easy -_-
//I used replaceAll to replace every space with _
//I used Copilot like a teacher to help me when I'm stuck
//one day I will create AI models -_-1
42 changes: 42 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,45 @@
// 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

//I got some help from CoPilot in debugging some parts.
// hard one

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("399p"));

//This part we are removing the last character from a string. We are removing p from 399p

//,padStart(3, "0"); it means make sure the string is 3 chaeacter ,
// if not, add zero to the front
//substring(0)
//paddedPenceNumberString.length - 2 means take all except the last two characters

//why do we do this code line? because pence is the decimals in pound
//so first we take all except the last two digets, means we take the pound part

//here we are dealing with the pence or decimal part of the pound
//line 35, 36 mean we create a variable pence and assign it the last two digits of (paddedPrnceString)

//here we make sure the pence value is 2 digits, if not add zero to the end

//this part is the money format, we created a template literal to add the value of pounds to pence
//we seprate them with a dot
10 changes: 8 additions & 2 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@ 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
//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
//pad(totalHour)
//

// c) What is the return value of pad when it 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

//pad(remainingSeconds)
// 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
//01
11 changes: 11 additions & 0 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.

//these are my commints
//we created a function with a parameter time to format 12 hour clock
function formatAs12HourClock(time) {
//we created a variable hours and assigned its value to be the object Number
//Number object can be any number
//to find number value we use the slice method to take the value from start to index 2
const hours = Number(time.slice(0, 2));
//here we created if statement, if the variable hours value bigger then 12
//then return the value of hours -12 ans add two 00z after with pm
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
Expand All @@ -23,3 +30,7 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

export {formatAs12HourClock};

// I will solve it later
Loading
Loading