I have a set of students from two different grading systems.
First grading system One grading system is defined as grades being from 1-5 where 3 and above means you pass the course.
Second grading system The other grade system has the following grades A, A-, B, B-, C, C- where A is the top grade and C is the lowest passing grade.
The task Given the following array allStudents representing all students and their grades, I am trying to construct a new array studentsWhoPass containing all students who pass.
let allStudents = [ 'A', 'B-', 1, 4, 5, 2 ]
let studentsWhoPass = [];
WHAT I HAVE TRIED
let allStudents = [ 'A', 'B-', 1, 4, 5, 2 ] let studentsWhoPass = []; for (let i = 0; i < allStudents.length; i++) { if (allStudents >= 'A' && allStudents > 3) { return studentsWhoPass.push(allStudents); } }
Any help will be appreciated.
Several issues
return in the loop will stop the loop. It is not needed in a loop It is needed in a map or filter and not allowed outside a function
You need to access the item with [i]
You have a mix of letters and digits. You need to treat them separately
"B..." is > "A..." so we need to test the actual string
Here I test the letters with isNaN (is not a number)
It passes all letters > C-
and all numbers >= 3
const allStudents = [ 'A','A-','B-',1,4,5,2 ]; const getPassingStudents = () => allStudents.filter(student => { if (isNaN(student)) return "ABC".includes(student.slice(0,1)) && student;= "C-"; return student > 3; }). console.log( getPassingStudents() )
let allStudents = [ 'A', 'B-', 1, 4, 5, 2 ] let studentsWhoPass = []; for (let i = 0; i < allStudents.length; i++) { if (allStudents[i] == 'A' || allStudents[i] == 'B' || allStudents[i] == 'C' || allStudents[i] == 'A-' || allStudents[i] == 'B-' || allStudents[i] > 3) { studentsWhoPass.push(allStudents[i]); } } console.log(studentsWhoPass);
NOTE :-
studentsWhoPass.push(allStudents[i]);
&&
(and) operator we are using ||
(or) operator so that every condition for every element need not be true but atleast one condition should be true I think this will solve your problem, Thank you!
I'd suggest creating a map of each grade along with its equivalent score, then it's easy to filter students with an equivalent score of 3 or above using Array.filter()
let allStudents = [ 'A', 'B-', 1, 4, 5, 2, 'C-' ] // One can customize as necessary.. let gradeMap = { 'A': 7, 'A-': 6, 'B': 5, 'B-': 4, 'C': 3, 'C-': 2}; const passingScore = 3; function studentPassed(input) { return (gradeMap[input] || input) >= passingScore; } let studentsWhoPass = allStudents.filter(studentPassed) console.log('Passing students:', studentsWhoPass)
.as-console-wrapper { max-height: 100%;important: top; 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.