DEV Community

mohandass
mohandass

Posted on

Array simple explanation with examples in j.s

  • An array in javascript is a special type of variable that can store Multiple value in single place.

Instant of creating many variables

You can use the array

  • Ordered - Items have positions "if called indexes, starting from 0"
  • You Can store any type - numbers, strings, objects, and other arrays
  • Dynamic - You can add/remove elements anytime

If you can using (const)

  • You cannot reassign the array
  • But you can modify the content inside the array

const marks = [70,50,30,60,80]
0 1 2 3 4
Using a bracket starting mark value is "0",second mark value is "1",third mark value is "2",fourth mark value is "3" and fifth mark value is "4"

HOW TO FIND THE COUNT OF FAIL ?

If using a array and while loop method to find the fail count

const marks = [70,50,30,60,80]
let fail = 0;
let i = 0;
while (i < 5){
if (marks[i] < 35){
fail++;
}
i++;
}
console.log("fail count" ,fail)

Output:
fail count 1

STEPS :
step 1:Declare a mark in array using (const marks) in square brackets "[]" to order the values

step 2: we want how many fail count in the marks.we don't know how many fail count.So declare the name fail "let fail = 0"

step 3: Then declare the first elements of array is index. so start form 0 "let i = 0"

step 4: Next use the while loop (i < 5) because the array have a five Elements the index starting from 0 to 4

step 5: and then declare the if condition if(marks[i]<35)

step 6: if the condition is true increase the fail count "fail++"

step 7: increase the i inside the looping bracket
Important point : increase the i into the if condition that is "infinity loop"

step 8: And then print the fail count "console.log("fail count", fail)"

How working process in this program

  1. we want the fail count above the mark.So declare the fail count is 0

2.Next declare the i = 0

3.if check the while loop condition
i=0
while (0 < 5)-condition is true

i=1
while(1 < 5)-condition is true

i=2
while(2 < 5)-condition is true

i=3
while(3 < 5)-condition is true

i=4
while(4 < 5)-condition is true

i=5
while(5 < 5)-condition is false so loop stop

  1. Next check the if condition

index 0=70
if(marks[70]<35)-condition is false

index 1=50
if(marks[50]<35)-condition is false

index 2=30
if(marks[30]<35)-condition is true

index 3=60
if(marks[60]<35)-condition is false

index 4=80
if(marks[80]<35)-condition is false

4.If the condition is true fail count is increase.the index condition 0,1,3,4 it's all false.If the fail count not increase
The index 3 only true so the fail count is increase fail count is 1

5.Then finally when the loop is stop.Then print the fail count

OUTPUT: fail count 1

How to find count of fail and pass

And find a total marks

Top comments (0)