Skip to content

NW7 | Sue Mohamadi | Module-Structuring-and-Testing-Data | module 2/ week 3 (Sprint-3 exercises) #114

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 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9e19f9c
a new dir called prep for ITP3
SueMsOl Oct 23, 2024
f99f9ca
identified and resolved the bugs inside files located in errors direc…
SueMsOl Oct 25, 2024
554cc2a
interpreted syntax / operators / functions inside interpret directory…
SueMsOl Oct 25, 2024
0ef45a8
looked up for unfamiliar syntax in exercises dir inside sprint 1 dire…
SueMsOl Oct 25, 2024
7a52923
Merge pull request #1 from SueMsOl/MSATD-sprint1
SueMsOl Oct 25, 2024
99071e5
finished tasks in interpret dir and explore dir in sprint 1 directory
SueMsOl Oct 25, 2024
8453583
Merge pull request #2 from SueMsOl:MSATD-sprint2
SueMsOl Nov 1, 2024
17c0a54
fixed 0.js error in errors dir
SueMsOl Nov 1, 2024
7a5a029
fixed error in 1.js file in errors dir
SueMsOl Nov 1, 2024
0110569
fixed error in 2.js file in errors dir
SueMsOl Nov 1, 2024
f56ab0a
fixed code in 0.js file in debug dir
SueMsOl Nov 1, 2024
bd89557
debugged 1.js file in debug dir
SueMsOl Nov 1, 2024
4ddc809
debugged 2.js file on debug dir
SueMsOl Nov 1, 2024
7937b18
debugged format-time.js file on extend dir
SueMsOl Nov 1, 2024
415d9bd
created a function for bmi.js in implement dir
SueMsOl Nov 1, 2024
576cf92
finished bmi function
SueMsOl Nov 3, 2024
f3e6bd0
finished cases function in cases.js
SueMsOl Nov 4, 2024
9e91758
finished toPounds function in pounds.js
SueMsOl Nov 4, 2024
45405ca
made to-Pounds function code DRYER
SueMsOl Nov 4, 2024
78d023c
finished writing function in vat.js
SueMsOl Nov 4, 2024
33f05f9
finished time-formats.js exercise
SueMsOl Nov 4, 2024
b55b233
fixed toPounds function bug in pounds.js
SueMsOl Nov 4, 2024
5092bca
fixed bug in pounds.js
SueMsOl Nov 4, 2024
87cdd24
fixed all bugs on pounds.js
SueMsOl Nov 4, 2024
b7695a7
made cases.js code dryer
SueMsOl Nov 6, 2024
3ce6b53
updated toPounds3 function in pounds.js
SueMsOl Nov 6, 2024
df206b2
I have installed jest framework, and ran example.test.js to see if it…
SueMsOl Nov 7, 2024
56f1cf1
wrote function in get-angle-type file and tested it using JIST framework
SueMsOl Nov 7, 2024
2ac16f3
wrote function in get-card-value file and tested it using JIST framework
SueMsOl Nov 7, 2024
fef5b77
wrote function in is-proper-fraction file and tested it using JIST fr…
SueMsOl Nov 7, 2024
0e7e294
wrote function in is-valid-triangle file and tested it using JIST fra…
SueMsOl Nov 7, 2024
a063e7d
wrote function in rotate.char.js file and tested it using JIST framework
SueMsOl Nov 8, 2024
fca2b8d
installed JIST framework in implement directory inside revise directory
SueMsOl Nov 8, 2024
518b44d
wrote 6 different functions in password-validator.test.js for each co…
SueMsOl Nov 8, 2024
7876ea0
an unresolved bug in passValidChecker function in password-validator.…
SueMsOl Nov 8, 2024
0dbd853
wrote functions required in 3 files inside revise/implement directory
SueMsOl Nov 9, 2024
f2d4b55
wrote functions required in repeat.test.js file inside revise/impleme…
SueMsOl Nov 9, 2024
b78e1aa
wrote functions required in is-prime.test.js file inside revise/imple…
SueMsOl Nov 9, 2024
7324324
wrote functions required in password-validator.test.js file inside re…
SueMsOl Nov 9, 2024
557660c
wrote functions required in password-validator.test.js file inside re…
SueMsOl Nov 9, 2024
f20d6a1
Merge pull request #3 from SueMsOl/MSATD-sprint2
SueMsOl Nov 10, 2024
2f572a7
Merge branch 'CodeYourFuture:main' into main
SueMsOl Nov 29, 2024
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
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?
because these sentences were not nested inside JS's comment syntax.*/
6 changes: 4 additions & 2 deletions Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/*when const is used to create a variable, it can not be reassigned. So for this practice, i replace
with let to fix this error.*/
let age = 33;
age = age + 1;
console.log(age);
6 changes: 5 additions & 1 deletion Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
/*the answer is related to the order that the code was written. the variable declaration followed console.log
instead of preceding it resulting in a reference error. I cut the variable declaration and pasted it above
console.log to resolve the problem.*/

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

7 changes: 5 additions & 2 deletions Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const cardNumber = 4533787178994213; // const cardNumber = `${4533787178994213}` also fixes the error by changing data type to string.
const last4Digits = cardNumber.toString().slice(-4);
console.log(`The last 4 digits of the card number are: ${last4Digits}`);

// 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?
/* the answer: slice() method does only work on strings and arrays. it does not work on numbers.
so I fixed it by changing the expression in Last4Digits variable. */
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
7 changes: 5 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
let HourClockTime = "20:53";
hourClockTime = "08:53";
console.log(hourClockTime); // Output: 08:53
/*Javascript variables can not be started with numbers. I changed const to let so that the variable can
be reassigned to a new value. */
2 changes: 2 additions & 0 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let count = 0;

count = count + 1;
/*line 3 reassigns the value that was passed onto count variable by using an arithmetic operator to add
1 to count variable's value.*/

// 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
8 changes: 7 additions & 1 deletion Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const num = 56.5678;

console.log(num);
// You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num )
// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num )
// 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 = Math.floor(num);
console.log("wHole number is:"+ wholeNumberPart);
const decimalPart = num - wholeNumberPart;
console.log("decimal part is:"+ decimalPart);
const roundedNum = Math.round(num);
console.log("rounded number is:"+ roundedNum);
4 changes: 4 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ let firstName = "Creola";
let middleName = "Katherine";
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.
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

console.log("initials are:" + initials);
16 changes: 14 additions & 2 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@
// (All spaces in the "" line should be ignored. They are purely for formatting.)

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
console.log(filePath);
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(lastSlashIndex);
const base = filePath.slice(lastSlashIndex+1);
console.log(base);
console.log(`The base part of ${filePath} is ${base}`);

// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
const dir = filePath.slice(0,lastSlashIndex+1);
console.log(dir);
// Create a variable to store the ext part of the variables.
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex+1);
console.log(ext);

//another way to extract ext part of filePath string is:
const ext2 = filePath.split(".").pop();
console.log(ext2);
6 changes: 5 additions & 1 deletion Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
/*due to rules of precedence, parenthesis executes first. Inside parenthesis, subtraction executes first,
then addition executes.*/
console.log("random number:" + num);
/*num keeps changing because Math.random() function outputs a different random value between 0 and 1 every time it runs. */

// In this exercise, you will need to work out what num represents?
// In this exercise, you will need to work out what num represents? answer: a numerical value
// 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
6 changes: 3 additions & 3 deletions Sprint-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Let's try an example.
In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
What effect does calling the `alert` function have? I get a pop-up window which prints my message.

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
What is the return value of `prompt`?
What effect does calling the `prompt` function have? I get a pop-up notification asking the question and providing a box to get the input.
What is the return value of `prompt`? it outputs the input of that question.
16 changes: 12 additions & 4 deletions Sprint-1/explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ In this activity, we'll explore some additional concepts that you'll encounter i

Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
What output do you get?
ƒ log() { [native code] }
(log is a function (that's what the ƒ symbol means).
It's a "native" function, meaning it's built into the browser's JavaScript engine)

Now enter just `console` in the Console, what output do you get back?
Now enter just `console` in the Console, what output do you get back?
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
console is an object.
It contains multiple functions (methods) like debug, error, info, log, warn, etc.

Try also entering `typeof console`
it returns object because console is an object.

Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
What does `console` store? methods. console is a toolbox full of different tools.
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
The dot tells JavaScript "look inside this object (console) and give me access to this specific method (log or assert)"
8 changes: 7 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 @@ -12,11 +12,17 @@ console.log(`The percentage change is ${percentageChange}`);
// Read the code and then answer the questions below

// a) How many function calls are there in this file? Write down all the lines where a function call is made
/*Line 4: Number() replaceAll() | line 5: Number() replaceAll() | line 10: console.log() */

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
/*Line 4; lack of comma between "," and ""; add a comma between them. */

// c) Identify all the lines that are variable reassignment statements
/*Line 4, line 5 */

// d) Identify all the lines that are variable declarations
/*Line 1, line 2, line 7, line 8 */

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*firstly, replaceAll string function gets executed. it replaces , in the carPrice variable's value with "". secondly, Number function changes
carPrice's data type from string to numeric. */
21 changes: 19 additions & 2 deletions Sprint-1/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@ console.log(result);

// For the piece of code above, read the code and then answer the following questions

// a) How many variable declarations are there in this program?
// a) How many variable declarations are there in this program? 6

// b) How many function calls are there?
// b) How many function calls are there? 1

// c) Using documentation, explain what the expression movieLength % 60 represents
/*it means dividing 8784 by 60 and output the remainder value of this division. */


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/*It calculates the movie length in minutes */

// e) What do you think the variable result represents? Can you think of a better name for this variable?
/*it shows how long the movie is in hour/minute/seconds format like 2:34:44 . A better name is: totalMovieLength */

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

/*an easier code would be:
const movieLength = 8784;
const totalMovieLength = `${Math.floor(movieLength / 3600)}:${Math.floor((movieLength % 3600) / 60)}:${(movieLength%3600) % 60}`;

Or
const movieLength = 8784;
const hour = Math.floor(movieLength/3600);
const minute = Math.floor((movieLength%3600)/60);
const second = (movieLength%3600)%60;
console.log(`movie length is ${hour}:${minute}:${second}`);
*/

14 changes: 8 additions & 6 deletions Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
// variable declaration; assignment of value 399p to penceString variable
const penceString = "399p";
// variable declaration; the expression takes the p out of penceString and outputs 399
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

//variable declaration; the expression adds 0 to the start of the penceStringWithoutTrailingP if it has less than 3 characters
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//variable declaration; the expression takes out 99 from paddedPenceNumberString and outputs 3
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

//variable declaration; the expression takes out 3 from paddedPenceNumberString, then adds 0 to the end of if its characters are less than 2
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
.padEnd(2, "0"); //output:99

console.log(`£${pounds}.${pence}`);

Expand Down
5 changes: 3 additions & 2 deletions Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

function multiply(a, b) {
console.log(a * b);
let multiplyOutput = Math.abs(a*b);
return multiplyOutput;
}

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, -5)}`);
3 changes: 1 addition & 2 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Predict and explain first...

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

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

const num = 103;
// const num = 103; this variable is a universal one. function needs its local variable.

function getLastDigit() {
return num.toString().slice(-1);
function getLastDigit(num) {
let lastDigit = num.toString().slice(-1);
return lastDigit;
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
Expand Down
9 changes: 6 additions & 3 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// 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(x) {
let z = `${x[0].toUpperCase()}${x.slice(1)}`; //inja age + bezaram toye output mishe + lol
return z;
}
/*khob error chi bood? i gave variable x to be my parameter. so now inside the function, if i want to create a new
variable, it needs a new name! */
console.log(capitalise("moein"));
16 changes: 13 additions & 3 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
// 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;
/* function convertToPercentage(decimalNumber) {
decimalNumber = 0.5; //remove this!
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(decimalNumber); //it must become console.log(convertToPercentage(0.25)); */
//correct version is written down:

function convertToPercentage(decimalNumber) {

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

return percentage;
}

console.log(convertToPercentage(0.8));
8 changes: 6 additions & 2 deletions Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

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

function square(3) {
/*function square(3) { //put a variable here for parameter! in this case, name in 'num'
return num * num;
} */
//correct version is written below:
function square(num) {
return num * num;
}

console.log(square(87));

26 changes: 26 additions & 0 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
// This is the latest solution to the problem from the prep.
// 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.

/* this function does not cover edge cases and minutes.
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}
*/
//this function below is an updated, advanced version of the function above.
function formatAs12HourClock(time) {
let converted = Number(time.slice(0,2));
let minutes = time.slice(-3);
//consider special cases first.
if (converted === 0 ) {
return "00:00 midnight";
}
else if (converted === 24) {
let specialCase24 = 24-12;
return `${specialCase24}${minutes} am`

}
else if (converted === 12) {
return `${converted}${minutes} pm`;
}
else if (converted>12){
let above12 = converted-12;
return `${above12}${minutes} pm`;
}
else{
let below12 = converted.toString().padStart(2,"0");
return `${below12}${minutes} am`;}
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
Expand Down
Loading