Skip to content

Commit

Permalink
chapter5 project complete & freecodecamp task planning
Browse files Browse the repository at this point in the history
  • Loading branch information
BrummieQuinn committed Aug 27, 2023
1 parent a078cac commit 81c86d5
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 1 deletion.
20 changes: 19 additions & 1 deletion chapter 5/chapter5.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,22 @@ for (let i = 0; i < 10; i++) {
console.log(string2);

// Chapter 5 Project:
// math multiplication table
// math multiplication table
// create blank array
let multiplicationTable = [];
let value = 12;
// create outer loop to iterate through each row
for (let i = 0; i < value; i++) {
// create temp array to store row values leave empty for now
let tempArray = [];
// create inner for loop for column values    
for (let j = 0; j < value; j++) {
//    push values to temp array  j * i     
tempArray.push(j * i);
}

//    push temp array to multiplication table array       
multiplicationTable.push(tempArray);
}
// log to check output
console.table(multiplicationTable);
88 changes: 88 additions & 0 deletions chapter 5/chapter5.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,4 +1089,92 @@ for (let group of groups) {
- final result will add row of values for the calculations

* Planning:
```Javascript
// create blank array
let multiplicationTable = [];
// specify how many values to multiply with one another
let value = 5;
// create outer loop to iterate through each row
for (let i = 0; i < 12; i++) {
// create temp array to store row values
let tempArray = [i];
// create inner for loop for column values
for (let j = 0; j < 12; j++) {
// create column variable for multiplication
let columns = j * value;
// push values to temp array
tempArray.push(columns);
}
// push temp array to multiplication table array
multiplicationTable.push(tempArray);
}
// log to check output
console.table(multiplicationTable);
```
*NOTE:* so this is another plan based on suggested steps.

I'm happy with it as far as the second for loop. But nothing different is coming to mind at present on how to change it so I guess its time to test and adjust

I'm learning to stay flexible

It's pretty important to have at least one idea to try - I think its just as important to have a toolbox like with css where I can keep trying different things to get my desired result if my first attempt doesn't work

I guess I just want to have core options I can manipulate to a solutution

I think I can use practice exercise 5.4 and 5.5 to help me build this,

(26/08/2023)
before going to sleep last night I decided to add a counter like with the previous exercise while it seems the right direction there are a few things I might have to tweak

Once again I was on the right path to a certain point:
- I didn't need the counter variable this time
- while the project was overall similar to the two previous practice exercises it wasn't the same in terms of needing a counter since the loop I was using both times did that for me, once that became clear I removed this
- not assigning i inside the tempArray = [i]
- if the counter was giving me output I didn't need, assingning i to the index filled the column with an extra row not needed which pushed the zeros column to the right leading to the multiplication tables not being in sync with the rows
- I really misunderstood that the value variable representented the times tables themselves not what is multiplied by
- since I know now that the value variable was referring to which multiplications tables (ascending) moving values into the middle for expression instead of hardcoding the rows and columns with actual number, this also had the benefit of freeing the .push()parentheses for the true calculation of j * i
- with these three changes I was able to build the multiplication table
- could I figure out something similar and equally open to misinterpretation? not confident

```Javascript
// create blank array
let multiplicationTable = [];
// specify how many multiplication tables e.g 1 * 1 - 12, 2 * 1 - 12 etc
let value = 5;
// let counter = 0; unecessary counter variable
// create outer loop to iterate through each row
for (let i = 1; i <= value; i++) {
// create temp array to store row values  - leave empty for next step  
let tempArray = [];

// create inner for loop for column values    
for (let j = 1; j <= value; j++) {
// create column variable for multiplication       
// let columns = j * value; didn't need the new columns variable or calculation
//    push values to temp array       
tempArray.push(j * i);
}
// counter++; (This counter is not actually necessary)
//    push temp array to multiplication table array       
multiplicationTable.push(tempArray);
}
// log to check output
console.table(multiplicationTable);
```

### summary:
- one thing I need to remember is when looping through an array containing an objects I can use the for in loop to extract the objects into declared variable.
- example
```Javascript
// first variable declared in for in loop - gives access to the objects held by the array
for (let i in contacts) {
// declare new variable here - variable will now hold reference to each object
let contact = contacts[i];
// use this variable to evaluate any conditional statements related to object contacts

}
```
- for/in loops over keys and for/of loops over values:
- super useful for iterating arrays and objects


237 changes: 237 additions & 0 deletions chapter 5/extranotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
# Extra notes:

so this page is for extra notes I make from other various resources.

I'm not too sure I'm completely confident applying what I've learned up to now.

I want Javascript to click and feel like I'm almost there, but I'm also aware that moving on too fast wouldn't be good either.

I'll also (maybe) look for some coding challenges that require me to use what I've learned to this point if possible

so this might also turn into a challenge planning page with a js file with solutions i've worked on

but yeah, not feeling too confident, so this:
***

## statements:

- statements can be compared to Javascript commands
- expressions are evaluated to produce value
- statements are executed to make something happen
- expression statement:
- an expression with side effects
- assignments
- function invocations
- can stand alone as a statement

- declaration statement:
- declare new variables
- define new functions

- Javascript programs are fundamentally a sequence of statements to execute
- due to interpreter default executing statement one after another, control structure e.g conditional/branching, jumps and loops/iteration

- conditionals/branching:
- if and switch statements
- execute or skip other statements depending on value evaluated by expression

- loops/iterations:
- while, do-while and for
- execute statements repetitively

- jumps:
- break, return and throw
- interpreter will jump to another part of program

### expression statements:

- simplest of statements are expressions with side effects
- example:
```Javascript
// assignment statements are expression statements
greeting = 'Hello ' + name;
// or
i *= 3;
// even
i++; i--; counter++
```
- increment and decrement assignments are changing the variable value as if assignment performed
- this side effect relates them to assignment statements

- delete operator has side effect of deleting 'object property'
*NOTE:* remember the above!!!
- used as statement rather than larger expression

- function calls are expression statements
- will have side effects that affect host environment or program state
- can be used as statements
- a function without a side effect, makes no sense to call
- a function without a side effect, if called, should be part of:
- assignment statement - as in call to assign result to a variable
- larger expression

### compound statements:
- statement block combines multiple statements into a compound statement e.g within {}
- primitive statements within block end with ';'
-{} do not end with ';'

- expressions can contain subexpressions
- statements can contain substatements
- the while loop syntax expect single statement to serve as loop body
- statement blocks allow the use of many statements within the single statement

- empty statements:
- allows the inclusion of no statements where one is expected
- use ';'
- useful to create a loop with empty body
- example:
```Javascript
// initialise array a
for (let i = 0; i < a.length; a[i++] = 0) ;
// no loop body necessary due to a[i++] = 0 ;
```
- javascript syntax a statement for loop body
- empty statement ; used so no loop body necessary
- if using comment code with clear purpose

### loops:
- common usage - iterate over array elements

- do-while:
- less used than while in practice
- do loop must end with ;
- while loop has no need if in {}

- for/of:
- loops through iterable objects
- arrays, strings, sets and maps
- with arrays:
- example:
```Javascript
// for of loop through array elements:
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9], sum = 0;
for (let element of data) {
sum += element
}
sum
// output: 45
```
- with objects:
- object not iterable by default
- would cause type error if for/of used directly
- use for/in or for/of Object.keys
- example:
```Javascript
let o = { x: 1, y: 2, z: 3 };
let keys = '';
for (let k of Object.keys(o)) {
keys += k
}
keys
// output: 'xyz'
```
- Object.keys() returns array of property names
- for/of iterates arrays

- Object.values() returns array of property values
- for/of iterates array

- Object.entries() returns array of arrays:
- each inner array contains keys and values
- one property of the object
- for/of iterates array

- for/in:
- works with any object after in
- example:
```Javascript
// assign property names of object to variable property
for (let property in object) {
console.log(object[p]);
}
```
- as I said I would I worked on the freecodecamp challenge to test my understanding

### freecodecamp task:
#### Profile task notes:

- What I know:

* The function should check if name is an actual contact's firstName and the given property (prop) is a property of that contact.

- If both are true, then return the "value" of that property.

- If name does not correspond to any contacts then return the string No such contact.

- If prop does not correspond to any valid properties of a contact found to match name then return the string No such property.



1. function needs to check 'if' 'name' is an actual contact's 'firstName' is a property of that contact
- represented by 'prop' in the function template
- use hasOwnProperty() method for this check?
- if (object.firstName.hasOwnProperty('firstName') {

2. 'if' both are 'true' then return the 'value' of that property
- if (object.hasOwnProperty('firstName') && 'name' === object.firstName ) {
return object.firstName.value;

} else if ('name' !== object.firstName) {
return 'No such contact';
3. 'if' 'name'does not correspond to any contacts, return the string 'No such contact'

} else if ('name' !== object.prop) {
return 'No such property';

4. 'if' 'prop' does not correspond to any valid properties of a contact found to match 'name' then return the string 'No such property'
}

});

### Trying to work it out:

object within array = [{}]

name = firstName 'string'

prop argument used with name to query object based on argument of 'name'

prop = lastName 'string'/number 'Number'\likes['string', 'string', 'string]

argument used with 'name' to query object properties where prop can be any property passed of particular nested object

1. check if name is an actual contacts 'firstName and the given 'prop' is a property of contacts

- use (.) notation to chain object properties and methods

- use conditional if() statement
- nested - k

```Javascript
// This function takes two arguments:
// argument 'name' checks existence of property in objects
// argument 'prop' retrieves the value of property if it exists
function lookUpProfile(name, prop) {
// Only change code below this line
// use for/in loop to iterate array to access elements (objects)
for (let i in contacts) {
// declare new variable and assign current object from array
let contact = contacts[i];
// check if new contact object contains 'firstName' property that matches 'name' argument
if (contact.firstName === name) {
// check if property exists in the new contact object
if (contact.hasOwnProperty(prop)) {
// if property exists return the value
return contact[prop];
} else {
// returns after evaluating inner if conditional: if no property matches
return 'No such property'
}
}
}
}
// final return here:
// returns after evaluating outer if conditional: if no name matches 'firstName'
return 'No such contact'
```

11 changes: 11 additions & 0 deletions chapter 6/chapter6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="promptexercises.js"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions chapter 6/chapter6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// practice exercise 6.1:
// declare variables
let number1 = 10;
let number2 = 5;

/**
* @param {any} a
* @param {any} b
*/
// declare function accepts parameters a and b
function add(a, b) {
// body of function block
// declared variable to store the calculation
let result = a + b;
// console.log(result)
console.log(result);
// return the result
return result;
}
// call function with globally declared variables
add(number1, number2);
// call function with arguments
add(2999, 2364);

// practice exercise 6.3:

Loading

0 comments on commit 81c86d5

Please sign in to comment.