/
Javascript  Arrays Array definition & for loop Javascript  Arrays Array definition & for loop

Javascript Arrays Array definition & for loop - PowerPoint Presentation

faith
faith . @faith
Follow
75 views
Uploaded On 2023-11-04

Javascript Arrays Array definition & for loop - PPT Presentation

var quiz 85901000 creates an array var ex e x0 89 add the quiz grades quizTotal 0 for int i 0 i lt quizlength i quizTotal ID: 1028774

number array function arrays array number arrays function var quiz elements javascript equal index called quiztotal variable base dwarf

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Javascript Arrays Array definition &..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Javascript Arrays

2. Array definition & for loopvar quiz = [85,90,100,0]; // creates an arrayvar ex = [];ex[0] = 89;// add the quiz gradesquizTotal = 0;for( int i=0; i< quiz.length; i++) quizTotal= quizTotal + quiz[i];quizTotal = quizTotal – quiz.min();

3. IndexingIndexing is the process of creating a sequence of names by associating a base name with a numberEach indexed item is called an element of the base-named sequenceAn index is enclosed in [square brackets] in JavaScript

4. Arrays [1]In programming, an indexed base name is called an arrayArrays must be declaredIn JavaScript, arrays are declared:var <variable> = new Array(<number of elements>)Notice that Array starts with an uppercase “A”

5. Arrays [2]Variables either are or are not arraysvar week = new Array(7);week is the identifier being declared,new Array(7) specifies that the identifier will be an array variable.number in parentheses gives the number of array elementsTo refer to an array’s length, we use <variable>.length

6. Arrays [3]Rules for arrays in JavaScript:Arrays are normal variables initialized by new Array(<number of elements>)<number of elements> in the declaration is just that—the number of array elementsArray indexing begins at 0Number of elements in an array is its lengthGreatest index of an array is <number of elements> − 1 (because the origin is 0)

7. Arrays [4]Array reference consists of array name with an index [enclosed in brackets] Value to which the index evaluates must be less than the array’s lengthExample:var dwarf = new Array(7); dwarf[0] = "Happy"; dwarf[1] = "Sleepy"; The number in the bracket is called the subscript

8. Conditional Operators>= greater than or equal to> Greater than< less than<= less than or equal to== equal to!= not equal to

9. If elsevar points= 87;if points >= 90 grade = “A”;else if points>=85 grade = “A-”

10. Function definitionWrite a function that generates two random numbers between 0-10 and adds them and returns them.function addTwo(){ var num1 = Math.random()*10; var num2 = Math.random()*10; var sum = num1 + num2; return sum;}

11. Function callA function has to be called in order to be activated.var total = addTwo();

12. One more functionWrite a function that rolls a pair of dice: if the sum > 10 return 1Else returns 0;

13. SummaryWe studiedIf ..elseFor .. LoopFunctionRandom function