How to insert the value of an Array within another Array

Asked

Viewed 473 times

0

I’m trying to separate students according to their numbers in the notebook where students grade A would be students numbering over 170, students B students numbering 160 to 169 and students C numbering 150 to 159.

I’m trying to get you back on the console like this: student Receive [170, 170, 171, 187, 191] and with alunosB and C the same. more does not appear I did like this below but only appears me Undefined and when I put in the function parameter the variable alunoA= even appears something else and only the same number independent of the variable.

var numeroCardeneta = [170, 159, 151, 187, 156, 191, 165, 154, 167, 169, 171, 170, 160]

var alunosA = []
var alunosB = []
var alunosC = []

function classes(numeroCardeneta) {
    for (var i = 0; i < numeroCardeneta.length; i++){
        if (numeroCardeneta[i] >= 150 && numeroCardeneta[i] <=159){
            alunosNotaC.push(numeroCardeneta[i])
            return alunosNotaC
        } else if (numeroCardeneta[i] >= 160 && numeroCardeneta[i] <= 169){
            alunosNotaB.push(numeroCardeneta[i])
            return alunosNotaB
        } else if (numeroCardeneta[i] >= 170){
            alunosNotaA.push(numeroCardeneta[i])
            return alunosNotaA
        }
    }
}

console.log(classes(alunosNotaA))
  • 1

    Read your own code, what’s that code for return within each if? You are practically forcing the cycle to end at the first value, remove all these return, besides you are interned over the array you pass as parameter, which means that if it is empty that is the case of the example, nothing will work. If you need to iterate on numeroCardeneta you don’t pass alunosNotaA as a parameter

  • ok thanks for the advice, and I started studying javascript a little while ago. I will look no further wrong on this issue XD

1 answer

3


The problem with your code is that you are using the return within the repetition structure. This causes the program to exit the function and consequently, interrupt the repetition loop.

Another error of the code, is that you are passing as argument to the function the array alunosA, array that has no element. Therefore, the function will not return any value, making the printing undefined.

To fix the problem, remove all statements return of its function and pass as argument the array numeroCardeneta. Or better yet, in your case you don’t even need to create this parameter in the function, since you can access the array in the most internal scopes.

Below is the corrected code:

var numeroCaderneta = [170, 159, 151, 187, 156, 191, 165, 154, 167, 169, 171, 170, 160];

var alunosNotaA = [];
var alunosNotaB = [];
var alunosNotaC = [];

function definirClasses() {
    for (var i = 0; i < numeroCaderneta.length; i++){
    
        const numero = numeroCaderneta[i];
    
        if (numero >= 150 && numero <= 159) {
            alunosNotaC.push(numero);

        } else if (numero >= 160 && numero <= 169) {
            alunosNotaB.push(numero);

        } else if (numero >= 170){
            alunosNotaA.push(numero);
        }
    }
}

definirClasses();
console.log("Classe A:", alunosNotaA);
console.log("Classe B:", alunosNotaB);
console.log("Classe C:", alunosNotaC);

  • Hello thanks for the help, and I ask for more help I started now little to use the stackoverflow but I would have some article where I could go deeper or you advise somewhere so I can get better these details because I’m still very lay.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.