school/html-java-script/week7/3-2-1-discussion.org
2021-03-31 21:56:49 -04:00

5.9 KiB

Week 7 Discussion Notes

3 Learned

While loops

We set an iterator. We give it a condition which makes the program loop until until that condition is false. So in our think javascript example:

function sumTo(n) { let total = 0; let v = 1; while (v <= n) { total = v; v+; } return total; }

We make our iterator, v, which increments by 1 until it is greater than n. At which point it stops looping. Or, more explicitly, we iterate until v is not less than nor equal to n, at which point the while loop returns false and breaks the loop.

Array values

We learned that Arrays in Javascript are more than happy to take any type of data. So if we were to make an array of numbers, we could also throw some strings or other arrays in there. I'm guessing it has no probably with any datatype available being thrown into the array. All mixed up.

We access the elements of the array as is standard in most languages. The, someVar[myElement] style of accessing, and the myElement can be any expression that evaulates to an integer.

If we try to access an element that doesn't exist, javascript returns "undefined"

These higher level langauges make things much easier (and possibily messier, so we need to be careful) to work with. If we were to compare this to Java, we would never be able to mix primitive types such as integers and floats in the same array (not directly, anyway).

However, before I make mistakes I've made before, one thing is not very clear here, which I'll get back to at the end.

Logical operators

We also learned about logical operators which seems to be pretty standard. If we want to make sure multiple statements are true, we can use && (and logic), if we want to see if there are any true statments among more than one, we can use || (or logic), and we can use ! (not logic) for negation.

so: 1 && 1 && 1 => true 1 && false && 1 => false 1 && !1 && 1 => false 1 || !1 || 1 => true and so forth.

AND and OR also perform short circuit evaluation.

So in a series of AND, the first expression (if any) to show false will end evaluation of any other expressions. We know it's false already, so we stop.

Similarly, the first truth a series of OR statements finds is enough to satisfy the condition of true and prove that it can't be false (overall) and stop bothering with more checks of any remaining expressions.

2 Interesting

Interesting 1

One of those most intersting things about java script is that it has two methods of finding equality. One that is loose and will do automatic type conversion to find if the same idea is true, so… 3 == "3" results to true. Which can be nice, but also possibily confusing. Think java script went with always using the other method.

The other method doesn't do automatic type conversion. So, while: 3 = 3 and 3 = "3" will end up being true, but… 3 = 3 will be true, 3 is 3 is 3, but 3 = "3" will not be true since 3 is a number and "3" is a string.

Interesting 2

isNaN() is a nice function. Often I find something as simple as typechecking is far more effort than it should be. And, not that I can recall the specific example, but I seem to remember seeing NaN as an error quite often in whatever I was doing. But to check if something is a number, the function will be something like, isNumber(someVar), which is fine, but it's nice to see consistancy between the error NaN, and the function to check if something is not a number isNaN().

1 not clear

I see that if we call the slice method on an array, we get a copy of a subarray (or the whole thing) returned. So we know that we will make an actuall copy. So, let myNewSlice = someArray.slice(1, 3); Should make myNewSlice it's own array, and not a pointer to a slice.

So, and this is a problem that's tripped me up in at least Python and Java so far… If I were to do something like: let myTestElement = someArray[1]; does myTestElement contain a copy of whatever is in someArray[1]? Or do I just end up with a pointer/reference to that same block of memory?

Ultimatly, if I were to later do, myTestElement = 10, will that affect someArray[1]?

Reading on, it seems that two different strings that are assigned the same value, so let a="kack"; and let b="jack"; Javascript may have those both point to the same object, or perhaps it won't. We can't tell. However, I think javascript only does this with immuntable variables, but I'm not sure.

Later on, it does mention that let newVar = otherVar; Will result in both variables pointing to the same object. For me, this is generally undesireable, but reguardless, I still don't see an answer to my origional question here. I think it's best to assume that we are always making an alias, as they put it, if we assign a variable to anything other than new mutable data, and even in that case, we need to be careful.

Best to always to splice() and slice() if we want to ensure that we are getting old data into a new memory location.

We could run into a lot of trouble with this. If we were trying to find the maximum number in an array, and we started it out like normal with something like, let max = userInput[0], any changes to max could result in changing userInput[0] which may or may not matter. And would probably be a hard bug to find if it does matter. Actually, I'm not sure what javascript would do here, I guess it would just change the alias without changing the value, perhaps it would be fine. But if we were adding the numbers together, I guess userInput[0] would also be the total? Yea. I'm not sure.

I ran into this https://www.geeksforgeeks.org/reference-and-copy-variables-in-javascript/ which says that pass by value in the case of strings and numbers. However, in 8.8 it says "If we assign one variable to another, both variables refer to the same object" So… I guess they meant only in the case of arrays, or this other site I found is wrong.

Project

Use 2 point percision