You are on page 1of 17

Count

GRADE 8
Learning Intention : To develop efficient codes using accumulator
in the looping concept in JavaScript.

Learning Objective : To develop program codes to find the count using


looping concept in JavaScript.

Success Criteria :
✓ Know the syntax of for…loop for the looping concept.
✓ Understand how accumulator can be used to find the count.
✓ Develop & execute the code to display the sum and count using
accumulator and For loop.
✓ Trace the output for the given for…loop structure.
RECAP
An accumulator is a variable
that the program uses to
calculate a sum or product of a
series of values. A computer
program does this by having a
loop that adds or multiplies
each successive value onto the
accumulator.
An eg. Adding the values
12,7,9,10
What is the difference between sum
and count?
SUM is the process of adding of numbers. It answering
the question: HOW MUCH? Or, WHAT IS THE
TOTAL?

COUNT is the process of determining the number of


occurrences of an object or determines the total number
of something and answers the question HOW MANY?
Difference between sum and count
S.No. Student Name Computer Marks
1 Anika 99
2 Beena 100
3 Nakul 94
4 Akul 97
Count :4 Total : 390
Write the program code & dry run to find the count &
sum of numbers from 1 to 5.

SUM : COUNT :
<script> <script>
for(var a=1,s=0;a<=5;a++) for(var a=1,c=0;a<=5;a++)
{ {
s= s + a; c= c + 1;
} }
alert("Summation = " +s) alert(“Count = " +c)
</script> </script>
Write the program code & dry run to find the sum of
numbers from 1 to 5.

SUM : a a<=5 s s+=a Alert box


<script> 1 T 0 0+1=1
for(var a=1,s=0;a<=5;a++) 2 T 1+2=3
{
3 T 3+3=6
s= s + a;
4 T 6+4=10
}
alert(“Total = " +s) 5 T 10+5=15
</script> 6 F Total = 15
Write the program code & dry run to find the count of
numbers from 1 to 5.

COUNT : a a<=5 c c+=1 Alert box


<script> 1 T 0 0+1=1
for(var a=1,c=0;a<=5;a++) 2 T 1+1=2
{ 3 T 2+1=3
c= c + 1;
4 T 3+1=4
}
alert(“Count = " +c) 5 T 4+1=5
</script> 6 F Count = 5
Write the program code & dry run to find the count &
sum of numbers from 1 to 5.
<script> a a<=5 S+=a c+=1 Alert box
for(var a=1,s=0, c=0;a<=5;a++) 1 T 0+1=1 0+1=1
{ 2 T 1+2=3 1+1=2
s=s+a
3 T 3+3=6 2+1=3
c= c + 1;
4 T 6+4=10 3+1=4
}
alert("Sum = " +s) 5 T 10+5=15 4+1=5
alert(“Count = " +c) 6 F Sum = 15
</script> Count = 5
Write the program & dry run to accept the computer marks of
3 students. Calculate and display the total marks of the 3
students and display the number of students getting 100.
<!doctype html>
<html>
<head> <title>JS</title> </head>
<body>
<h1 style="text-align:center;"> Marks </h1>
<script>

</script>
</body> </html>
Write the program & dry run to accept the computer marks of 3 students. Calculate
and display the total marks of the 3 students and display the number of students
getting 100.
<script>
for(var a=1,s=0,c=0,m=0;a<=3; a++)
{
m=+prompt(“Enter your marks”);
s=s+m;
if(m==100)
c= c + 1;
}
alert(“Total= ” + s);
alert(“Count= ” +c);
</script>
Write the program & dry run to accept the computer marks of 3 students. Calculate
and display the total marks of the 3 students and display the number of students
getting 100.
Dry Run : Assume m=100, 98, 95
<script>
a a< m s=s+m m= c=c+ Output a++
var s=0,c=0,m=0; =3 =10 1 (Alert)
for(var a=1;a<=3; a++) 0
{ 1 T 100 0+100= T 0+1 1+1=2
m=+prompt(“Enter your marks”); 100 =1
s=s+m; 2 T 98 100+98 F 1 3
if(m==100) =198
c= c + 1; 3 T 95 198+95 F 1 4
} =293
alert(“Total= ” + s); 4 F Total=293
alert(“Count= ” +c); Count=1
</script>
Assess your Learning
Write the program & dry run to accept the cost of 4 items
purchased by the user. Calculate and display the total cost and
display the number of items costing 50dhs or more.
<!doctype html>
<html>
<head> <title>JS</title> </head>
<body>
<h1 style="text-align:center;"> Cost of Items </h1>
<script>

</script>
</body> </html>
Assess your Learning

Write the program & dry run to


accept the name & age of 4 users.
Check and display the name and age
of users above the age of 21. Also
display the total number of users
above the age of 21.
This Photo by Unknown Author is licensed under CC BY-SA

You might also like