JSNotes 3



JSNotes 3More on local and global variablesWhen there is a naming conflict the name applies to the most recent named value: see (new) Chapter 1 program arith_functions1.htmlLook at 3 functions in item 6 of index – GlobalChange, moreGlobalChange and Scope_of_VarsThe Math object.The Math object of JS is similar to the Math module of Python: It contains the usual assortment of math functions – trig functions, raising to a power, square roots, etc.You do not need to import the Math object, but you need to call its functions as follows: let maxy = Math.max(x, y, z):The most common ones you will use are : Math.max(), Math.min(), Math.round(), Math.random()As in Python, Math.random() will produce a number between 0 and 1.If you examine the code in the addition1.html page in part B of the html files at you will be reminded of how to obtain a random number between any two integers of your choice.Math.round() also is similar to the same function in Python. Again, these functions round to the nearest integer. If you examine the code at the rounding.html file in the same folder then you will recall that to round x to 3 decimal places, you multiply x by 1000, round to the nearest integer, and then divide by plete information about the Math object is (of course) on the MDN site at but by now you should be pretty good at searching for that site.Strings, Arrays and SortingAgain, the situation in JavaScript is similar to that in Python. In an array, there is a sort method and sorting is done using the ASCII collating sequence. (Python) or UT-16 collating sequence (JavaScript) This means that all upper case letters come before lower case ones.For example, "Cherry" comes before "banana" and "can do" comes before "candid". (The blank space comes before all letters and numerals.)It also means that sorting for numbers is lexicgoraphic. That is numbers are converted into strings and then sorted. So 50 comes before 7.The solution for this is shown in ---- namely: For arrays of numbers, if you want to use the built in sort() method for arrays, you should provide a comparison function as the optional parameter to the sort method For strings make everything all upper case or all lower case. For proper names, life is even more complicated. The ALA standard is that apostrophes and hyphens are ignored. ("O'Hara" is the same as "OHara") and also capitalization is ignored. (They are also the same as "ohara".) So you need to do some 'cleaning' before you compare. Even more complicated, there are special rules for names using other alphabets or for alphabetizing by family name, given name when the person wrote in a language other than English.If you don't find this clear, you may like (but the part about typed arrays is more advanced and may be skipped.) Now look at StringMethods, StringCompares and sorting, sorting2 and the array1 thru array4 code. You should be able to explain what is happening.Objects, Arrays and Deep Copy.This is another issue which is probably familiar to you from Python.Simple values (such as a number or boolean) are "pass by value". That is, if you have some number stored in x and you say let new_x = x;then whatever value is stored in x will be copied into new_x. If you change the value of x, you have NOT changed the value of new_x.That is NOT the case for compound data structures such as arrays and objects. These are "pass by reference" structures. If I have some array Arr and I say let new_Arr = Arr;Then new_Arr will be just another name (think of it as a pen name) for Arr. When you change the value of Arr[0] you are also changing the value of new_Arr[0].And, of course, we may have an array of objects or an array of arrays, etc. So the problem is how to make a "deep copy" (all the way down thru all the levels) of an array or object.Solution 1: Using JSON, turn your object/array into a string, make a copy of the string, and then using JSON turn it back into an object. This works so long as you don't have any Dates, RegEx, etc. in your object: Please see this page for restrictions. But the code is easy: let new_obj = JSON.parse(JSON.stringify(obj))Solution 2: Use jQuery or the lodash library – also detailed at that pageSolution 3: If you wish to copy an object or array which you know has only one layer, then iterate thru it and copy each entry. This is done in the sorting.html script you looked at a few minute ago.Summary/ Things to remember:Think about the scope of your variables – use let, const, var as appropriate. (Remember that vars are hoisted.)The Math object has many useful math functions.If A and B are arrays then A=B makes A another name for B – i.e. not a separate copy of B.If you want a separate copy of B you need to make “a deep copy” (i.e. element by element).length is a property of an array – e.g. A.lengthsort() is a method of an array – e.g. A.sort() sort() is a sort-in-placesort() is lexicographic (alphabetical)if you want a better sort (usually) then you need to write your own comparison function and pass it to sort (as an optional parameter)-e.g. A.sort(myCompare) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download