Javascript - If and Loop

Asked

Viewed 452 times

-1

I’m trying to answer a question inside a platform, and I’m not being able to adjust the code as the algorithm wants. I am receiving the following errors : 1- You must have an if validating the counter i/ 2- You must print the message "So-and-so resident can use the elevator". Any hint?

Question :

Over the weekend, our building’s elevator broke down, severely restricting the flow of people. Considering this, only the residents of the apartments with even number will be able to use the elevator. Write a code that, based on the variable "residents" in which residents can use the lift. The apartment number is its position in the array, and the resident’s name is the value.

Print the list on the console with the following phrase: 'The resident ' + address name + ' you can use the lift'

// MY CODE :

var moradores = [
    "Fulano de Tal",
    "Beltrano da Cia",
    "Viajante do Tempo",
    "Morador da Lua",
    "Marciano Azul",
    "Et da Eslováquia",
    "Jedi do Lado Cinza da Força",
    "Baby Yoda Amarelo"
]

for ( var i = 0; i < moradores.length; i++){
    if( moradores[i] % 2 || moradores[i] == 0){
        var nomes = moradores[i]
    }
    
}
    console.log("O morador " + nomes + " pode usar o elevador")

3 answers

1


So-and-so is the resident of apartment 1, the couple resident begins with Beltrano da Cia. There is, apartment 0, so it is as follows

var moradores = [
    "Fulano de Tal",
    "Beltrano da Cia",
    "Viajante do Tempo",
    "Morador da Lua",
    "Marciano Azul",
    "Et da Eslováquia",
    "Jedi do Lado Cinza da Força",
    "Baby Yoda Amarelo"
]

var nome = '';

for ( var i = 0; i < moradores.length; i++){
    if(i % 2 == 0){
       // ##### Vai de escada ou fica no apartamento :-)
    }else{
        nome = moradores[i]
        console.log("O morador " + nome + " pode usar o elevador")
    }
    
}

0

As you want to find the PAR NUMBER resident, you have to do the tests with the 'i' counter, which you are testing with your ifs are the names, which doesn’t help you solve the problem. Another problem, you’re creating the variable names inside the loop, overwriting it with each iteration, and printing after all this, so only the last resident saved in the variable will be printed, instead of all the qualified.

I suggest changing the code to something like:

var moradores = [
    "Fulano de Tal",
    "Beltrano da Cia",
    "Viajante do Tempo",
    "Morador da Lua",
    "Marciano Azul",
    "Et da Eslováquia",
    "Jedi do Lado Cinza da Força",
    "Baby Yoda Amarelo"
]

var nome = '';

for ( var i = 0; i < moradores.length; i++){
    if(i % 2 == 0 || i == 0){
        nome = moradores[i]
        console.log("O morador " + nome + " pode usar o elevador")
    }
    
}

Any questions regarding the code, you may ask!

  • I edited to fix a mistake I’d made

  • I appreciate the explanation and the suggestion, I actually modified the code and the terminal is printing correctly, but the algorithm of the platform I’m using still indicates an error : Must have an if validating the i counter . @placementw

  • @placementw Tbm created a global scope variable without any need!

  • 1

    so-and-so is the resident of apartment 1, the couple resident begins with Beltrano da Cia. Does not exist, apartment 0

  • @Thank you Leocaracciolo, that was really the problem.

0

You might want to consider using filter, so a new array will be created only with the results that fit your condition!

var moradores = [
    "Fulano de Tal",
    "Beltrano da Cia",
    "Viajante do Tempo",
    "Morador da Lua",
    "Marciano Azul",
    "Et da Eslováquia",
    "Jedi do Lado Cinza da Força",
    "Baby Yoda Amarelo"
];

let moradoresPermitidos = moradores.filter((a, i) => i % 2 === 1);
for (const nome of moradoresPermitidos) {
    console.log(`O morador ${nome} pode usar o elevador`)
}

  • Thank you for the suggestion, but the algorithm would not accept the resolution.

Browser other questions tagged

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