Skip to content
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

Glasgow_Class|Shreef_Ibrahim|Structuring _Testing _Datast|Week1_exercise1-update #234

Open
wants to merge 5 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
8 changes: 6 additions & 2 deletions Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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?
*/

// To include instructions or comments in code for human readers without executing them.
4 changes: 3 additions & 1 deletion 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;
/* The value of a constant cannot change through re-assignment,
we can use var or let insted*/
let age = 33;
age = age + 1;
13 changes: 11 additions & 2 deletions Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
/*
we try to print the value of variable `cityOfBirth` before declaring it.
JavaScript throws a reference error because the variable `cityOfBirth` has not been
defined at that point.*/

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

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

8 changes: 7 additions & 1 deletion Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().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

/*because slice() is method of String not a Number.
It extracts a section of a string and returns
it as a new string, without modifying the original string.
*/
console.log(last4Digits) // expected output 4213
9 changes: 7 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";
/*
Variable names must begin with a letter,
underscore, non-number character. Each language has its own conventions.*/
const HourClockTime12 = "08:53";
const hourClockTime24 = "20:53";
4 changes: 3 additions & 1 deletion 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;
count++;

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

// eassigning the count variable to a new value by adding 1 to its current value.
8 changes: 8 additions & 0 deletions Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ 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
let wholeNumberPart = Math.floor(num);

var decimalPart = (num - Math.floor(num)).toFixed(4);

var roundedNum = Math.round(num);
console.log(`Whole Number Part: ${wholeNumberPart}`); // Expected: 56
console.log(`Decimal Part: ${decimalPart}`); // Expected: 0.5678
console.log(`Rounded Number: ${roundedNum}`); // Expected: 57
3 changes: 3 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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.
var initials = firstName[0] + middleName[0] + lastName[0];
console.log(initials);

6 changes: 6 additions & 0 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const lastDotIndex = filePath.lastIndexOf(".");
const base = filePath.slice(lastSlashIndex + 1);
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

var dir = filePath.slice(0, lastSlashIndex);
var ext = filePath.slice(lastDotIndex);
console.log(dir)
console.log(ext)
8 changes: 7 additions & 1 deletion Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

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

// The variable num represents a random integer between 1 and 100.
// so each time you run the program, a different integer within the range [1, 100] will be generate.
Comment on lines +10 to +11
Copy link

Choose a reason for hiding this comment

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

To test your understanding, how would you write an expression
(without using any variable in the expression) that yields a random
integer between -5 and 3 (including -5 and 3)?

Copy link
Author

Choose a reason for hiding this comment

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

Math.floor(Math.random() * (3 + 5 + 1)) - 5;

5.66;
console.log(num);
let x = Math.floor(Math.random()*(3 + 5 + 1))-5
console.log(x);
9 changes: 7 additions & 2 deletions Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ 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;

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
// There are five function calls in the file.

// 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 6, the replace call function takes tow prameter there is no comma,to fix you should add a comma between the arguments

// c) Identify all the lines that are variable reassignment statements
// line 4 and 6

// d) Identify all the lines that are variable declarations
// line 1, 2, 8 and 9

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// firsly its will remove the comma and convert the result to a number /the purpose is to get a number insted of str
12 changes: 9 additions & 3 deletions Sprint-1/interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = 999999; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
console.log("the total" + totalMinutes);

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);

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

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

// b) How many function calls are there?

// 1
// c) Using documentation, explain what the expression movieLength % 60 represents
// 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.fb
Copy link

Choose a reason for hiding this comment

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

What does this particular remainder represent?

Copy link
Author

Choose a reason for hiding this comment

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

Represents the remaining minutes that are not part of a full hour when the movie length is expressed in hours and minutes.


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// calculate the total minutes by subtracting remainingSeconds from movieLength and then dividing by 60 to convert the remaining seconds into minutes

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// its using tamplat to store variable and any tex /the better name could be movieRuntime

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// yes will work/but with negative numbers gives unexpected resultscls
14 changes: 14 additions & 0 deletions Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
3;

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
Expand All @@ -25,3 +26,16 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2.const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// The substring() method her returns the part of this string from the start index up to lenght-1 => last one not inclouded then
// initialises a string variable assign to the result of supStr.
// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// initialises a string variable with value of the result of padStart this case it will
// retun the value of three index form the started of Str =>399.
// 4.const pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2);
// samilar to 2 just in this case last to index not inclouded
// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// this will finsh i 4 steps A/initialises a string variable/B- git the value (paddedPenceNumberString.length - 2 == 1)and take us arg
// C/ calc paddedPenceNumberString.substring(1)=="99" and the resultof padEnd() = 99 asign tp the pence
Comment on lines +38 to +39
Copy link

Choose a reason for hiding this comment

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

Can we expect this program to work as intended even if we deleted .padEnd(2, "0") from 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.

yes, it will work because it formatted when added earlier padStart(3, "0").

// 5.console.log(£${pounds}.${pence});
// This line outputs the formatted price in pounds and pence using template literals.