Link to home
Start Free TrialLog in
Avatar of sunilgohel
sunilgohel

asked on

How to get total count of passed in subject based on marks?

I have array of marks for one student like below
var marks = [15,38,35,25,19,28];

Now I want to get the total count of passed in subject in which marks above minimum passing marks (28)
For e.g. 38+35+28 >=28 = 3

Need solution in javascript or jQuery.
Avatar of Rgonzo1971
Rgonzo1971

HI,

pls try
var count = 0;
for(var i = 0; i < marks .length; ++i){
    if(marks [i] >= 28)
        count++;
}

Open in new window

Regards
@Sunilgohel, you can use foreach loop of jQuery for simpler solution!

Please find below code like (not tested, but may works for you!)
//Call function to get total count of passed subject
var totalMarsk = marks.reduce(getCountOfArray);

//Function to manipulate passes subject and count
function getCountOfArray(total, num) {
    return total + (num>=28)?1:0;
}

Open in new window


Hope you get idea from this!
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well what you'll need to do is use a for loop to iterate over each mark then check if it's over 28, if it is leave it, if not drop it from your array.  there's a pop function that will do that for you, once you've done this the length property will give you the number of subjects your student passed.

@everyone, this is homework, be a professional maybe instead of providing the answer give the student some advice and let him or her learn something.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunilgohel

ASKER

Thanks for your help Rgonzo and Prakash for giving example and Pawel for explaining logic to understand!