Help with a javascript code (Beginner)

Asked

Viewed 94 times

0

They gave me an exercise but when I send it to see if it’s right they tell me it’s wrong, that it’s not returning the correct value. To be more exact they claim that:

- The function summeDe25(10) must return 2.5

moedas = 0
function somarMoedasDe25 (quantidadeDeMoedas){
  for (let i = 0; i < quantidadeDeMoedas; i++){
    moedas = moedas + 0.25
  }
  return moedas
}

But I do tests and when I put 10 as a parameter it returns 2.5

inserir a descrição da imagem aqui

As a beginner, I’m not sure if it’s my code that’s really wrong. Can you help?

  • 1

    They who? With what you posted, you can’t say much. What we know so far is that the function summeDe25(10) returns 2.5, so it is in the post. Missing ; in your JS, but this should not be a hindrance (the conceptual error is the JS allow, but then runs out of our reach). Another problem is that you initialize moedas out of function, so if you do the test twice then you will understand the problem.

1 answer

2


I imagine that if you are submitting this code for some automatic test, this test will call your function several times with different parameters.

Like moedas is declared in the global scope, this value will only be incremented as a result of your function calls, see the example:

moedas = 0
function somarMoedasDe25(quantidadeDeMoedas) {
  for (let i = 0; i < quantidadeDeMoedas; i++){
    moedas = moedas + 0.25
  }
  return moedas
}

console.log('O resultado da soma de 5 moedas é:', somarMoedasDe25(5))
console.log('O resultado da soma de 10 moedas é:', somarMoedasDe25(10))

You should probably declare moedas within its function to have no side effects:

function somarMoedasDe25(quantidadeDeMoedas) {
  let moedas = 0
  for (let i = 0; i < quantidadeDeMoedas; i++){
    moedas = moedas + 0.25
  }
  return moedas
}

console.log('O resultado da soma de 5 moedas é:', somarMoedasDe25(5))
console.log('O resultado da soma de 10 moedas é:', somarMoedasDe25(10))

You could also return quantidadeDeMoedas * 0.25 instead of using a for, This is part of the exercise?

  • Suggestion would be to make the console.log twice with the same value (10) in your example, to be more evident the side effect. However, +1 is already given

  • Thank you, that’s right. Yes, it is part of the exercise to use "for". I just don’t quite understand why your different result declaring the variable in the global scope.

  • @Todeshine in the global it is zeroed only at the initialization of the code, and only increases. Inside it always starts at zero at each function call.

  • @Todeshine, it is because your variable is not zeroed when you invoke the function for the second time, it is loading the result of the previous function.

  • aaaaaaaaaaaah, now I understand. Thank you very much!

  • @Todeshine you can mark the answer as accepted by clicking on the green V side of the dots as soon as the minimum system time passes.

  • @user140828 Do you have any website or software where I can test my codes without giving this kind of error? I was using https://playcode.io/

  • @Bacco all right, thanks! I’m new here, still can’t move very well.

  • I didn’t quite understand what you’re looking for @Todeshine, the error was in the code, not on the site, the result will be the same.

Show 4 more comments

Browser other questions tagged

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