Arrays and programming styles Flashcards
zero based array
- the first value in an array is 0, not 1
first slot would have an idea of 0
2nd slot would have an index of 1
3rd index would have an index of 2
etc..
array
a collection of values wrapped up under one named variable
programming language with a 1 based index (1)
visual basic
how to write an array in javascript (easy way)
var a = [ ]; a[0] = Apple; a[1] = iPod; a[2] = 77;
now, a[0] = Apple and so on.
how to write an array (fast way)
var a = [“Apple”, “iPod”, 77]; [0] [1] [2]
an array is often referred to as
a collection
in javascript, arrays are…
objects
if there are 5 items in an index, the highest # index is
4
0, 1, 2, 3, 4
method
a function that belongs to an object
what does the .reverse method do?
suppose:
a.reverse ( );
it would reverse the order of the array
Apple(0), iPad(1), 77(2)»_space; 77(0), iPad(1), Apple(2)
what does .sort method do?
suppose:
a.sort ( );
it would sort an array based on the rules of the programming language
alphabetical, numeric, or a mixture.
what does .join method do?
suppose:
a.join ( );
returns an array separated with commas as a character string
what does .pop method do?
suppose:
var b = a.pop ( );
would take the last value of an array, remove it from the array, and set b = the last value of the array that was just removed
what does .push method do?
suppose:
a.push (123);
123 would be added as a new index to he array in a new index slot
who wrote the “book” on javascript?
Mozilla, they own it.
iteration
a loop
mutable object
- an object that can mutate or change
immutable object
- an object that cannot change or mutate
in javascript, the arrays are … mutable or immutable?
mutable
NOTE* javascript is the exception to the rule. most programming languages do not allow you to modify arrays once they are created
what are the benefits of immutable arrays?
- faster
- less prone to error – an array for a week would want 7 days or 7 values. it would never make sense to have an array for days of the week to change.
Generally, you want your code to be (3)
1) readable
2) consistent
3) accepted best practice
camelcase
highScore
uglyDuck
mySQL
cityNailAndSpa
The standard for variable naming in javascript
javascript (5) best practices
1) use camel case for variables, functions, and methods
2) open curly braces on the same line
3) define function before you call them
4) always use semicolons at the end of your statement
5) always use var when creating a variable
pseudocode
the process of writing down your code in plain english to see the problems you have to solve