Skip to content

London| Emmanuel Gessessew | Module-Structuring-and-Testing-Data/ sprint 2 | WEEK 2 #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 5ca380
5 changes: 3 additions & 2 deletions Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
// This is just an instruction for the first activity - but it is just for human consumption
// We don't want the computer to run these 2 lines - how can we solve this problem?
// comments
10 changes: 8 additions & 2 deletions Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
// const age = 33;
// age = age + 1;


// the problem is the variable age is declared as const which mean we can not change it to fix this we change it from const to let

let age = 33;
age = age +1;
7 changes: 6 additions & 1 deletion Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
// console.log(`I was born in ${cityOfBirth}`);
// const cityOfBirth = "Bolton";

// the problem is we cannot access city of birth before initialization, here is the correct one

const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
12 changes: 10 additions & 2 deletions Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
// const cardNumber = 4533787178994213;
// const last4Digits = cardNumber.slice(-4);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Before running the code, make and explain a prediction about why the code won't work
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value


// the code provided will produce an error cause the .slice() will only generate strings and arrays while the cardNumber is a number.
// to correct this i will introduce the .toString() to change it to a string,

const cardNumber = 4533787178994213;
const last4Digits = cardNumber.toString().slice(-4);
console.log(last4Digits);
14 changes: 12 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";

// here the problem is a variable name in java script can not start with a number and also the reading in time 20:53 is in the 24 hr clock time


const twelveHourClockTime = "08:53";
const twentyFourHourClockTime = "20:53";

console.log(twelveHourClockTime);
console.log(twentyFourHourClockTime);

2 changes: 2 additions & 0 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// in line 3 the code is adding 1 to the variable count, that is after the line 3 is executed the value of count will now be 1.
// the = operator is used to assign a value to a variable.
4 changes: 4 additions & 0 deletions Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ const num = 56.5678;
// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number )

// Log your variables to the console to check your answers

const wholeNumberPart = console.log(Math.floor(56.5678));
const decimalPart = num - wholeNumberPart;
const roundedNum = console.log(Math.round(56.5678));
5 changes: 5 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ let lastName = "Johnson";

// Declare a variable called initials that stores the first character of each string.
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.

// to select the first character of each variable i will use the charAt() also known as character at method which is used to extract a character at a specified index.
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
// The console.log function in JavaScript is used to print (or log) messages to the console.
console.log(initials);
18 changes: 18 additions & 0 deletions Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing

// answer
//here the num is any integer between the value of 1 to 100, to breakdown the code
// the function Math.random() calls any number between 0 to 0.9999. lets say 0.5575


// and this Math.random() * (maximum - minimum + 1) calculates the random number we got by (maximum - minimum + 1), which can be (100-1+1) which will give us the result of 100.


// (0.5575 *100) = 55.75


// the function Math.floor will round the result of the previous step down to the nearest whole number, giving a random integer between 0 and 99 which in this will be 55.


// finally add the minimum to the result

console.log(num);
19 changes: 18 additions & 1 deletion Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -20,3 +20,20 @@ console.log(`The percentage change is ${percentageChange}`);
// d) Identify all the lines that are variable declarations

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

//answers

// a)
//on line 4, carPrice.replaceAll(",", ""), replaceAll is called on carPrice to remove the commas.
// on line 4, Number is called to replace the results of replaceAll to a number, cause they were strings.
// on line 5, priceAfterOneYear.replaceAll("," ""), replaceAll is called on priceAfterOneYear to remove thee comas.
// on line 5, Number is called to replace the results of replaceAll to a number, cause they were strings.
// on line 10, console.log(`The percentage change is ${percentageChange}`);, console.log is called to print the percentage.

// b) the error is in line 5, priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); a comma is missing between the two strings, the correct one is priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// c) in line 4 and 5 the variables carPrice and priceAfterOneYear are replaced to a new values. first they are both strings with commas now they are numbers.

// d) in javascript let and const are used to declare variables, so in the above code we have 4 declared variables: carPrice, priceAfterOneYear, priceDifference and percentageChange.

// e) Number(carPrice.replaceAll(",","")): lets break it down. first the expression replaceAll(",","") is removing the comma (it was "10,000", now it is "10000"). and the second expression Number(carPrice.replaceAll(",","")) is changing the string to number(it was "10000" now it is 10000 )
14 changes: 14 additions & 0 deletions Sprint-1/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ console.log(result);
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

//answers

// a) there are 6 variable declarations

// b) there is one function, console.log(result); which prints the result.

// c) the expression movieLength % 60, uses the % modular which returns the remaining of a division. here 8784 is the total movieLength in seconds and 60 is total seconds in one minute, so the total expression is saying how many seconds are remaining after we divide 8784 by 60 which will be 24 seconds.

// d) line 4 calculating the the total number of whole minutes in the variable movieLength which is in seconds.

// e) variable result represents time of the movieLength in hours:minutes:seconds. another name can be movieDuration.

// f) i tried different values and the code works well.
14 changes: 13 additions & 1 deletion Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ console.log(`£${pounds}.${pence}`);
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 1. const penceString = "399p": initializes a string variable with the value "399p"

// to help me understand first i googled words that are new to me. penceString means the variable is a string, substring means a portion of a string, withOutTrailing mean describing a variable without a certain character, padded or pad means simply adding to a string.

// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);, creates a new variable called penceStringWithoutTrailingP by removing the last character p, resulting in "399".

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");, pads the string penceStringWithoutTrailingP with a leading "0" if it is less than 3 characters long, making it at least 3 characters. here "399" is three characters so it remains unchanged.

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);, will create a variable called const pounds y taking the characters from the start of paddedPenceNumberString up to but not including the last two characters which will result in "3".

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");, will create a variable called const pence this line of code extracts the last two characters from a padded string representation of pence and ensures that it is represented as a two character string.

// 6. console.log(`£${pounds}.${pence}`);, will print the amount of money in pounds and pence using template literals and then it outputs that formatted string to the console.
11 changes: 10 additions & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Predict and explain first...

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

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

Choose a reason for hiding this comment

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

It would be very beneficial for your learning to formally write out your prediction before changing the code.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I'll make sure to write out my predictions before making any changes in the future to better understand the code and improve my learning process.


// // correction
// the multiply code will not work cause the return is not defined. replace return (a*b) instead of console.log(a*b).
function multiply(a, b) {
console.log(a * b);
return a * b;
}

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

16 changes: 13 additions & 3 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// Predict and explain first...

// function sum(a, b) {

// return;
// a + b;
// }

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

// correction
// the sum(10,32) in the console.log will give an undefined result. a + b should be on the same line of code with return.

function sum(a, b) {
return;
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)}`);
25 changes: 19 additions & 6 deletions Sprint-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// Predict and explain first...

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

function getLastDigit() {
// 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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

// correction
// when the num is defined in the above code it uses the const it should use the let, since getLastDigit is not a constant number.
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I don't think let or const is salient here.

Do you think you need to assign 103 to num? Why would you need to do this?

Copy link
Author

Choose a reason for hiding this comment

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

how about this, function getLastDigit(num) {
return num % 10;
}

// aan also getLastDigit in the function should have a parameter (num), else it will only give the last digit of the hardcoded num which is 103.
let 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
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
12 changes: 11 additions & 1 deletion Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
// 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;
// }

//correction
// the problem is the capitalise function is that it is re declaring the str parameter inside the function using let.
// we canot do that cause the str is already a defined function.
Copy link
Member

Choose a reason for hiding this comment

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

It's not a defined function. It is defined, but what is it, exactly? Try to be precise here in your explanation.

Copy link
Author

Choose a reason for hiding this comment

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

function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}, wat if i do this


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

Choose a reason for hiding this comment

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

Do you need to assign this to str? Have a think

Copy link
Author

Choose a reason for hiding this comment

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

i don't need to assign the capitalized string back to str. Instead, function capitalise(str) {
return ${str[0].toUpperCase()}${str.slice(1)};
}

return str;
}
console.log(capitalise("emmanuel"))
17 changes: 15 additions & 2 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
// Why will an error occur when this program runs?
// 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);

//correction
// the problem in the above code is that it redeclaring the decimalNumber with const.
// and the The variable decimalNumber is only available inside the function calling it outside will result in error
Copy link
Member

Choose a reason for hiding this comment

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

What kind of error?

Copy link
Author

Choose a reason for hiding this comment

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

The error will be when u encounter if you try to access decimalNumber outside the function is a ReferenceError


function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;

const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(0.5));
11 changes: 9 additions & 2 deletions Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

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

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

// correction
// the problem in the above code is it didnt defined the num as a parameter of square.
Copy link
Member

Choose a reason for hiding this comment

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

Can 3 ever be a parameter in JavaScript? What kind of error did this code produce?


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


console.log(square(3))
2 changes: 2 additions & 0 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ console.assert(
currentOutput2 === targetOutput2,
Copy link
Member

Choose a reason for hiding this comment

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

What happened here? Do you need help with this one?

`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// input 1,
21 changes: 21 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place


// solution

function calculateBMI(weight, height) {

const heightSquared = height * height;


const bmi = weight / heightSquared;


return parseFloat(bmi.toFixed(1));
}


const weight = 70;
const height = 1.73;
const bmi = calculateBMI(weight, height);
console.log(`Your BMI is: ${bmi}`);

19 changes: 19 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution


//solution

function toUpperSnakeCase(input) {

const withUnderscores = input.replace(/ /g, "_");
Copy link
Member

Choose a reason for hiding this comment

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

Nice!



const upperSnakeCase = withUnderscores.toUpperCase();


return upperSnakeCase;
}


console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));

Loading