DEV Community

Deva I
Deva I

Posted on

Basic About Array in Javascript and Some Examples

DEFINITION OF ARRAY:

Array is an ordered collection of values that can store multiple data types under a single variable name.

CHARACTERISTICS OF ARRAY:

◾Zero Indexed:
In the array the first element is always at index 0 and the second at 1,and so on.

◾Dynamic Sizing:
In Arrays automatically as you add or remove elements.

◾Heterogeneous:
A single array can hold a mix of different types, such as numbers, strings, and even other arrays.

CREATING AND WORKING OF ARRAY:

Creation: Use the common Array Literal notation: const mobile brand = ["Apple", "Realme","Vivo"];

Working: Use bracket notation: mobile brand [0] returns "Apple".
mobile brand [1] returns "Realme".
mobile brand [2] returns "Vivo".

FIND THE COUNT OF FAIL:

PROGRAM:

STEPS:
Step 1: Declare a marks in array.Notation is const marks[marks]

Step 2:We wanna how many count the students fail.so declare a global variable named let.we don't know about the count so, declare let fail=0.

Step 3: Then declare i=0.because the first element of array is index 0.

Step 4:Next write the while loop(i<5). because the array have five elements and index starting 0 to 4.

Step 5:Then declare the if(marks[i]<35)

Step 6:If the condition is true increase the fail count.

Step 7: Increment the i

Step 8:Print the fail count.

WORKING:
1.We wanna fail count so declare fail count is 0.

2.Then check the while condition:
i = 0
While(0<5).True
i = 1
While(1<5).True
i = 2
While(2<5).True
i = 3
While(3<5).True
i = 4
While(4<5).True
i = 5
While(5<5).false.loop stop

3.Next check the if condition:

Index 0=80
if(marks[80]<35).false

Index 1=98
if(marks[98]<35).false

Index 2=89
if(marks[89]<35).false

Index 3=33
if(marks[33]<35).true

Index 4=100
if(marks[100]<35).false

4.If the condition is true fail count is increase.In the above condition index 0.1,2,4 is false. so fail count not increase.Index 3 is true,then fail count increase.Fail count is 1.

5.Finally when the while loop fails it prints the fail count.

OUTPUT:

FIND THE COUNT OF PASS AND FAIL:
PROGRAM:

STEPS:

Step 1: Declare a marks in array.Notation is const marks[marks]

Step 2:We wanna how many count the students fail and pass count.so declare a global variable named let.we don't know about the count so, declare let fail=0 and pass=0.

Step 3: Then declare i=0.because the first element of array is index 0.

Step 4:Next write the while loop(i<5). because the array have five elements and index starting 0 to 4.

Step 5:Then declare the if(marks[i]<35)

Step 6:If the condition is true increase the fail count.

Step 7:6:If the condition is false increase the pass count.

Step 8: Increment the value of i

Step 9:Print the fail count.

WORKING:

1.We wanna fail and pass count so declare fail and pass count is 0.

2.Then check the while condition:
i = 0
While(0<5).True
i = 1
While(1<5).True
i = 2
While(2<5).True
i = 3
While(3<5).True
i = 4
While(4<5).True
i = 5
While(5<5).false.loop stop

3.Next check the if condition:
Index 0=80
if(marks[80]<35).false

Index 1=98
if(marks[98]<35).false

Index 2=89
if(marks[89]<35).false

Index 3=33
if(marks[33]<35).true

Index 4=100
if(marks[100]<35).false

4.If the condition is true fail count is increase.In the above condition index 0.1,2,4 is false. so fail count not increase.Index 3 is true,then fail count increase.Fail count is 1.

5.Then if condition is false increase pass count.Index 3 is true and except all marks are pass.so pass count is 4

6.Finally when the while loop fails it prints the fail and pass count.

OUTPUT:

FIND THE TOTAL MARKS:

PROGRAM:

STEPS:

Step 1: Declare a marks in array.Notation is const marks[marks]

Step 2:We wanna total of the student.marks.so declare a global variable named let.we don't know about the total so, declare let total=0.

Step 3: Then declare i=0.because the first element of array is index 0.

Step 4:Next write the while loop(i<5). because the array have five elements and index starting 0 to 4.

Step 5:Then declare total= total+marks[i].

Step 6: Increment the value of i

Step 8:Print the total count.

WORKING:
1.We wanna total so declare total is 0.

2.Then check the while condition:
i = 0
While(0<5).True
i = 1
While(1<5).True
i = 2
While(2<5).True
i = 3
While(3<5).True
i = 4
While(4<5).True
i = 5
While(5<5).false.loop stop

3.Then check the condition:
total= total+marks[i].
Total=0+80 =80.total is 80

Total=80+98 =178.total is 178

Total=178+89 =267.total is 267

Total=267+33 =300.total is 300

Total=300+100 =400.total is 400

4.Increment the value of i.

5.Then print the total marks 400.
OUTPUT:

Top comments (0)