We help IT Professionals succeed at work.

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

sunilgohel
sunilgohel asked
on
118 Views
Last Modified: 2017-03-23
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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
HI,

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

Open in new window

Regards
Prakash SamariyaIT Professional
CERTIFIED EXPERT
Top Expert 2015

Commented:
@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!
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
PawełI Design & Develop Software

Commented:
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.
IT Professional
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
PawełI Design & Develop Software
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for your help Rgonzo and Prakash for giving example and Pawel for explaining logic to understand!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.