Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Custom counting method in JavaScript

For learning purposes I've created a JS-File. I implemented a Method inside the JS-Object to get a BookStorage counter. The thing is everytime I add Books the Browser gives me back the default value for the Object. Anyone have an Idea?

Here is some snippets of my Code:

Book-Class:

class Book{
constructor(
    name,
    quantity
){
    this.name = name,
    this.quantity = quantity
}
showStorage(param) {
    if(param != null){
        return this.quantity + param;
        this.quantity += param;
    }
        else {return this.quantity;}
    }
}

Main-Script:

    import Book from "./Book.js"

const lordOfRings = new Book(
  "Lord of the Rings",
  10
)

console.log("The Book:", lordOfRings.name);
console.log("The number in Storage:", lordOfRings.showStorage());
console.log("Adding 10 Books to storage:", lordOfRings.showStorage(10));
console.log("Adding 10 Books to storage:", lordOfRings.showStorage());
console.log("New Storage amount of Books:", lordOfRings.showStorage(10));

My expected output was:

"Lord of the Rings"
10
20
20
30

Instead of that I get:

"Lord of the Rings"
10 
20 
10 
20 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is because in a function, the return should be the last line.

The return statement ends function execution and specifies a value to be returned to the function caller. (MDN Docs)

Since the function execution ends, it means the code after the return statement is never executed.

I recommend debugging your code using a code execution visualizer

Here's part of your code. You tried to change the value of this.quantity after the return statement.

showStorage(param) {
    if(param != null){
        return this.quantity + param; // This doesn't change the value of this.quantity. It just returns that value + 10 (in your example). The value of `this.quantity` is always the same.

        // This line does not run because it's after the return statement
        this.quantity += param;
    }
        else {return this.quantity;}
    }
}

Here's a minimal fix. You need to increment the value of this.quantity, then return it.

showStorage(param) {
    if(param != null){
        this.quantity += param;
        return this.quantity;
    }
        else {return this.quantity;}
    }
}

Here's a refactor taking into account comments from Ivar and Cristian Traìna

showStorage(param) {
    if(param){ // an empty `param` is `undefined`, not `null` as the commenter said above.
        this.quantity += param;
    }
    return this.quantity;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda