Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
How can I use a For-Loop to return a new array from an old one

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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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()
)

about 4 years ago · Juan Pablo Isaza Denunciar

0

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:-

  • return statement can only be there in any function any outside the function, you can just update and push the value into the array
  • You were trying to push whole array put here we'll just push the element in the array e.g. studentsWhoPass.push(allStudents[i]);
  • In the if statement, you were checking whether the whole array for a certain condition but you will have to check for the element same like the above example
  • for checking for the character, we could check using the ASCII code but it could become confusing, to make it simple we are checking for individual letters
  • Also, instead of &&(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!

about 4 years ago · Juan Pablo Isaza Denunciar

0

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; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda