Skip to content

ITP_GLASGOW_MAR | HANNA_MYKYTIUK | MODULE_STRUCTURING_AND_TESTING_DATA | SPRINT_1 #433

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 6 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
4 changes: 2 additions & 2 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let count = 0;
let count = 0; //Variable "count" created and assigned value equal to 0.

count = count + 1;
count = count + 1; // Variable "count" assigned new value, equal old value + 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
4 changes: 3 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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 = ``;
let initials = firstName.charAt(0)+middleName.charAt(0)+lastName.charAt(0);
console.log(initials);


// https://www.google.com/search?q=get+first+character+of+string+mdn

9 changes: 7 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ 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 = ;
const ext = ;

const dir =filePath.slice(1,lastSlashIndex) ;
console.log(`****The dir part of ${filePath} is ${dir}`);
const lastIndexOfDot = filePath.lastIndexOf(".");
const ext = filePath.slice(lastIndexOfDot);
console.log(`**** The ext pert of ${filePath} is ${ext}`);

Choose a reason for hiding this comment

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

This is the answer this exercise is looking for as it teaches you basic string manipulation, but if you are doing this for real, use a library (you'll get to those shortly - when you have, google path.parse )


// https://www.google.com/search?q=slice+mdn
9 changes: 8 additions & 1 deletion Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
for ( let i = 0; i < 1000; i++){
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num);
}
// Math module used to generate random integer in range from 1 to 100, and assign it to variable "num"
// function ".random()" gives random float number fron 0 to 1
// after this value multiplayed onto "(maximum - minimum + 1)" wich with give constants is equal to 100
// function ".floor()" returns the largest integer less than or equal to a result of previous multiplication.

// In this exercise, you will need to work out what num represents?
// Try breaking down the expression and using documentation to explain what it means
Expand Down
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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?
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-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;
let age = 33;
age = age + 1;

console.log(age);
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

2 changes: 1 addition & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213";
const last4Digits = cardNumber.slice(-4);

// The last4Digits variable should store the last 4 digits of cardNumber
Expand Down
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const TwelveHourClockTime = "20:53";
const TwentyFourHourClockTime = "08:53";
11 changes: 6 additions & 5 deletions Sprint-1/3-mandatory-interpret/1-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,12 @@ 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

// Three functions calls in lines 4,5,10
// 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?

// The error in line 5

Choose a reason for hiding this comment

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

You've identified the error, but haven't made a fix

// c) Identify all the lines that are variable reassignment statements

//Reassignment in line 4,5
// d) Identify all the lines that are variable declarations

//The variable declarations are in line 1,2,7,8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//This code removes comas from prices before conversion to numbers.
13 changes: 7 additions & 6 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 50; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -12,14 +12,15 @@ 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?

// Thera are 6 variable declarations
// b) How many function calls are there?

// Thera is one function.
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// It shows the number of full minutes in movie
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// It shows the lengs of movie in format hours:minutes:seconds
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// It will works
6 changes: 6 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 3. const penceStringWithoutTrailingP declared and assighed value - substring of penceString that's
// starts with 0 and end withouts last chracter
// 8. const paddedPenceNumberString declared and assighed value - If string penceStringWithoutTrailingP shorter than
// 3 characters, it add 0 to the starts.
// 14. const pence declared and assighed value - substring from variable paddedPenceNumberString ( last two characters)
// If string pence shorter than 2 characters, it add 0 to the end.
2 changes: 2 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
//Pop up window with hello message appears.

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`?
//Pop up window with input field appears, after input function returns inputed string into Console.
10 changes: 9 additions & 1 deletion Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ 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?
// We got output like: ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
// We got: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`
//We got: 'object'

Answer the following questions:

What does `console` store?
What does `console` store?
// Console store object, wich has methods to display different messages in console;
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
//'.' - allows us to use functions and properties of console object.
// `log` and `assert` are these functions
// `console.log` to display console messages
// `console.assert`to write an error message to the console in case of assertion failed
13 changes: 9 additions & 4 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle < 90) return "Acute angle";
if (angle >90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle >180 && angle < 360) return "Reflex angle";
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +46,16 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse,"Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(200);
assertEquals(reflex, "Reflex angle");
10 changes: 8 additions & 2 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
}
if (numerator < denominator) {
return true;
} else if ( numerator >= denominator) {

Choose a reason for hiding this comment

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

Why do you need the second if test?

return false;
}
}

// here's our helper again
function assertEquals(actualOutput, targetOutput) {
Expand Down Expand Up @@ -40,13 +44,15 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction,true );

Choose a reason for hiding this comment

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

This code has given you one case with negative numerators. Can you construct a test case with a negative numerator that you expect to return false. Does it?

// ====> complete with your assertion

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);
// ====> complete with your assertion

// Stretch:
Expand Down
30 changes: 28 additions & 2 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
if (rank === "A") return 11;
let rank;
if (card === "10"){
rank = card;
} else {
rank = card.charAt(0);
}

Choose a reason for hiding this comment

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

Consider getCardValue("10♥")

How does your code behave?

if (rank === "A") {
return 11;
} else if (Number(rank) >= 2 && Number(rank) <= 9) {
return Number(rank);
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
throw new Error("Invalid card rank");
}
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +48,30 @@ assertEquals(aceofSpades, 11);
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(fiveofHearts, 5);

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const faceCard = getCardValue("10");
assertEquals(faceCard, 10);

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const handleAce = getCardValue("A");
assertEquals(handleAce, 11);

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
const invalidCard = getCardValue("g");
assertEquals(invalidCard, "Invalid card rank.");
} catch(error){
console.log(error.message);
}

13 changes: 4 additions & 9 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement

if (angle < 90) return "Acute angle";
if (angle >90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle >180 && angle < 360) return "Reflex angle";
}








// Don't get bogged down in this detail
// Jest uses CommonJS module syntax by default as it's quite old
// We will upgrade our approach to ES6 modules in the next course module, so for now
Expand Down
24 changes: 12 additions & 12 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ test("should identify right angle (90°)", () => {
// REPLACE the comments with the tests
// make your test descriptions as clear and readable as possible

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angle (< 90°)", () => {
expect(getAngleType(40)).toEqual("Acute angle");
});

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
test("should identify obtuse angle (> 90° and < 180°)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");
});

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test("should identify straight angle (180°)", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
test("should identify reflex angle (> 180° and < 360°)", () => {
expect(getAngleType(200)).toEqual("Reflex angle");
});
7 changes: 5 additions & 2 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (numerator < denominator) {
return true;
} else if ( numerator >= denominator) {
return false;
}
}

module.exports = isProperFraction;
9 changes: 9 additions & 0 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("should return true for a improper fraction", () => {
expect(isProperFraction(5, 3)).toEqual(false);
});

// Case 3: Identify Negative Fractions:
test("should return true for a negative fraction", () => {
expect(isProperFraction(-2, 3)).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return true for a equal fraction", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});

Choose a reason for hiding this comment

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

Re-read your code carefully here. I'm guessing that you wrote this test by copying and pasting the previous test and changing it a bit? Which is common programmer practice, but a frequent source of bugs. If you do it, be careful, as the problem you have here is all too common. Also common when correcting this sort of error is to make the wrong correction!

Loading