Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 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 2f6225
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ let count = 0;

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
// Ans : Line 3 is adding 1 to the variable. = is assigning variable value.
9 changes: 8 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,14 @@ 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 = ``;
// declaring variables for each name and take out fist character

let fname_initials = `${firstName.charAt(0)}`;
Copy link
Member

Choose a reason for hiding this comment

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

In JavaScript we tend to use camelCase not snake_case for variable names, so this would be fnameInitials rather than fname_initials. This isn't a big deal - the code works either way, but it's a convention we tend to stick to.

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 advice .
I have changed all the variable names as camelCase.

Copy link
Member

Choose a reason for hiding this comment

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

Because charAt returns a string, we don't need to interpolate this into a string as well

Suggested change
let fname_initials = `${firstName.charAt(0)}`;
let fname_initials = firstName.charAt(0);

Copy link
Author

Choose a reason for hiding this comment

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

Remove string interpolate.

Copy link
Member

Choose a reason for hiding this comment

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

This string will contain exactly one character, so initial is probably a better than than initials, which suggests it contains more than one :)

Copy link
Author

Choose a reason for hiding this comment

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

noted and changed as per suggestion . Thank you.

let midname_initials = `${middleName.charAt(0)}`;
let lastname_initials= `${lastName.charAt(0)}`;

console.log("The letters are ",fname_initials,midname_initials,lastname_initials);


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

9 changes: 6 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

// (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";
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.jpeg";
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

const dir = ;
const ext = ;
const dir = filePath.slice(0,lastSlashIndex);
console.log (`The dir part of the file path ${dir}`);
const ext = filePath.slice(lastDotIndex+1);
console.log(`The ext part of file path ${ext}`);

// https://www.google.com/search?q=slice+mdn
16 changes: 16 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ const maximum = 100;

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


const step1 = Math.random(); // get random number >=0 & <1
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a good explanation, breaking it down into steps is really useful, but can you also talk about what the whole thing does, as well as just the separate steps?

Copy link
Author

Choose a reason for hiding this comment

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

As a whole thing this program is for testing Operator precedence , in order to know which operation performs fast . I answered as per I understand :)
Please guide me if I am in the wrong way .. Thank you .

Copy link
Member

Choose a reason for hiding this comment

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

Precedence is one of the things being tested here, but general code understanding is too :) Can you try to also describe this code in terms of the problem?

e.g. in the initials exercise you could describe in problem terms we're "Making a string of the first letter of each name" (whereas in programming terms we'd maybe say "We're making variables storing the first character of each name, and then concatenating them together into one variable")

Copy link
Author

Choose a reason for hiding this comment

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

We are making a variable storing the result of round down to nearest largest number of the random numbers multiplied by the result of maximum number subtract minimum , after that adding by 1 and then adding with minimum value.

console.log(`The random number is ${step1}`);

const step2 = (maximum-minimum+1);
console.log(`The second step is ${step2}`); // subsctract (max from min) and add 1

const step3 = step1*step2; // random number multiply by the result from step2
console.log(`The third step is ${step3}`);

const step4 = Math.floor(step3); // round down the number and return largest or equal to given number
console.log (`The fourth step is ${step4}`);

const step5 = step4 + minimum //add with the minimun number
console.log (`The final step is ${step5}`);

// 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
Expand Down
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?

//Ans: comment the 2 lines

2 changes: 2 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

const age = 33;
age = age + 1;

// The error is cause of trying to change the constant variable
2 changes: 2 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

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

// The error is because of accessing the variable (cityOfBirth) before declaration .
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213"; // adding double code to solve the error
const last4Digits = cardNumber.slice(-4);

console.log(`The last 4 digits are ${last4Digits}`);

// The error is cause of function call on the wrong object (integer)

// 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
Expand Down
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-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";
const 24hourClockTime = "08:53";

// the error is because of variable naming conversion . The variable names are staring with numbers.
// it should start with letter or underscore or dollar sign.
7 changes: 6 additions & 1 deletion 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,16 @@ 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
// 2 functions calls are there . Line 7 and 8
Copy link
Member

Choose a reason for hiding this comment

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

I don't think line 7 or 8 have function calls, but I do see several in the file - can you look again?

Copy link
Author

Choose a reason for hiding this comment

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

I have checked and modified it . Please check and let me know . Thank you .


// 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?
//Error at Line 5. There are is cause of missing (,) in replace method . It can be fixed by putting (,) between arguments at replace method()

// c) Identify all the lines that are variable reassignment statements
// Line 4 & 5

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

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Removing the (,) in carPrice string and changing to number in order to get number value.
10 changes: 7 additions & 3 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 = 100784; // length of movie in seconds

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

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

//no function calls
Copy link
Member

Choose a reason for hiding this comment

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

This isn't the case - how do you identify a function call?

Copy link
Author

Choose a reason for hiding this comment

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

When there is a block of code starting with the function key word in the program which is to solve a specific problem , I identify it as the user defined function .
And a function call is getting a value by using that function name only (no need to write the whole code) in another part of the code where needed .
As I see in the program I can find out by assigning 6 variables with some operation ..

I shared my point of view . Please correct me and explain to me . Thank you so much .

Copy link
Member

Choose a reason for hiding this comment

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

There are also functions that the user didn't define, but that already exist in the language.

A function call generally looks like a pair of ()s after a name (possibly with some values between the ()s).

Do you see any places in this code where there's a name, and then a (, then maybe some values, then a )?

This would still be a function call, even if the user didn't define the function themselves.

Copy link
Author

Choose a reason for hiding this comment

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

I can see log (result ) method . So there is one function call in this program .
Kindly check and guide me on this .

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

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// substract remaining minutes from total minuts and divided by 60

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// duration will be better

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// yes it is working with different values.
7 changes: 7 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ const penceStringWithoutTrailingP = penceString.substring(
penceString.length - 1
);


const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

console.log (`The result is ${paddedPenceNumberString}`);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
Expand All @@ -25,3 +28,7 @@ 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) : removing p by using substring method.
// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); : adding 0 to the start from the left side in case of penstring have 2 numbers only
// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); : getting the first letter from the string
Copy link
Member

Choose a reason for hiding this comment

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

The first letter? What's in the string? Why would we be getting the first letter from it?

Copy link
Author

Choose a reason for hiding this comment

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

Getting the first character from the string to get the pound value from the given string penny .

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); // get the last two letter and add zero in case if there is only one letter.
4 changes: 4 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,12 @@ 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 dialog box with the message "Hello World" and Ok button.

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?
pop up dialog box with text box to accept user input

What is the return value of `prompt`?
string is the return value of prompt
Copy link
Member

Choose a reason for hiding this comment

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

What is returned if the user presses cancel?

Copy link
Author

Choose a reason for hiding this comment

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

null value if user press cancel or enter without typing any value in text box.

5 changes: 5 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ 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?
// ƒ log() { [native code] }

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

Try also entering `typeof console`

Answer the following questions:

What does `console` store?
store data for a while
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by "a while"? Where does it store data?

Copy link
Author

Choose a reason for hiding this comment

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

It stores data temporarily on the browser's memory when it is open and disappear when refreshing the browser .


What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
// (.) is for accessing the object's method
17 changes: 13 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> capitalise the first letter and take out as it from second till end .

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

const Fname = capitalise("sarawone");
console.log(`The result is ${Fname}`);

// =============> write your explanation here
// =============> write your new code here
// Ans: the error is cause of assigning the same variable in again in the function (str)
// =============>
// function capitalise(str) {
//let name = `${str[0].toUpperCase()}${str.slice(1)}`;
// return name;
//}
18 changes: 13 additions & 5 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Predict and explain first...
// Will get syntax error

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> Cause same variable (decimalNumber) is reassign in the function body

// Try playing computer with the example to work out what is going on

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

return percentage;
}

console.log(decimalNumber);
let number = convertToPercentage(0.5);
console.log(`The result is ${number}`);

// =============> write your explanation here

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>
//function convertToPercentage(decimalNumber)
// {
//const percentage = `${decimalNumber * 100}%`;
//return percentage;
//}
//let number = convertToPercentage(0.5);
// console.log(`The result is ${number}`);
18 changes: 13 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

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

// =============> write your prediction of the error here
// =============> there is no assign value for num variable

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

// =============> write the error message here
let result = square(3);
console.log (`The result is ${result}`);

// =============> explain this error message here
// =============> SyntaxError: Unexpected number

// =============> This error cause because there was a number in the place where it is not expected by parser.

// Finally, correct the code to fix the problem

// =============> write your new code here
// =============> function square(num) {
// return num * num;
// }

// let result = square(3);
// console.log (`The result is ${result}`);


11 changes: 8 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// =============> no return in function so it will show error

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

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

// =============> write your explanation here
// =============> it shows as undefined

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> function multiply(a, b) {
//console.log(a * b);
//return a*b;
// }

11 changes: 6 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I think it will return the input parameter numbers

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

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

// =============> write your explanation here
// =============> it returned undefined , there is not return value assign in the function body.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> function sum(a, b) {
// return a+b;
// }
14 changes: 8 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> I think it will return only 3 for every input .

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

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

Expand All @@ -14,11 +14,13 @@ 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
// =============> 3
// Explain why the output is the way it is
// =============> write your explanation here
// =============> The function do not take any parameter and it is taking the constant variable return the last digit.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> function getLastDigit(num) {
//return num.toString().slice(-1);
// }

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