Javascript function

Asked

Viewed 583 times

-5

inserir a descrição da imagem aquiI need to create a function doubleDoProximo() that takes a parameter and returns us twice as many times as the next one, i.e., parameter + 1. so I did :

function dobroDoProximo (numero1, numero2) {
   var numero1 * numero2 ;
   return  dobroDoProximo +1;
}

but you’re not going, can anyone help please ?

  • 2

    Put some example of the output you want, it is very confusing the text.

  • I added an image, see if it helps

  • 3

    I just think it’s bad that the solutions people give always involve unnecessary variables and more complex solution than it should, sometimes for lack of math, and that some say it’s more readable to write more code (Writing unnecessary code was never readable, but now is the preferred excuse).

  • 2

    If it were Black Friday it would be metadeDoDobro()

4 answers

4

Uncomplicated:

Since no one posted the most basic code, here goes:

function dobroDoProximo(atual){
   return (atual+1)*2;
}


Demonstration:

function dobroDoProximo(atual){
   return (atual+1)*2;
}


console.log(dobroDoProximo(4));   // tem que ser 10
console.log(dobroDoProximo(0));   // tem que ser 2
console.log(dobroDoProximo(417)); // tem que ser 836

  • 2

    You could write in other ways, but you would lose the semantics. Ex: return atual*2+2; which gives the same result but hinders readability. As well as putting variable randomly also bothers me in this case, because the operation is too simple to ensure the legibility and interpretation of the intention.

2

You need to create a variable (var) that sums the next parameter+1 and dps return the variable multiplying by 2. At least it works that way for me, since I have the slight impression that this platform has some bugs with the console, based on me going back to an exercise, that was correct, modify it and return to "correct" and give error, even restarting the console. _(ツ)_/

function dobroDoProximo(numero1){
   var soma = numero1+1;
   return soma*2;
}
  • 2

    It worked the/ , I never imagined it to be this way because I was thinking that I had to take the variable with multiplication and add +1 . Thank you!!

  • 2

    No need to use intermediate variables, are processing cycles and memory allocation that can be avoided in this case.

1

Some things do not need to decorate too much. Simple and objective.

function dobroDoProximo(numero1){

   return numero1 * 2 + 2
}

-2

You need to assign the number multiplication value to another variable.

function dobroDoProximo (numero1, numero2) {
   var dobro = numero1 * numero2 ;
   return  dobro +1;
}

I believe the problem statement is asking for a function that multiplies the number by 2 and adds one more to that value. So the function would be something like this:

function dobroDoProximo(proximo) {
 var dobro = 2 * proximo;
 return dobro + 1;
}
  • Exactly that --> "I believe the problem statement is asking for a function that multiplies the number by 2 and adds one more"--- but it didn’t work as you did

  • I’m not the one who was negative, but I’m almost negative. No need to use intermediate variables, are processing cycles and memory allocation that can be avoided in this case.

  • 2

    @Augustovasques also was not I, but has a reversal in the order of operations. It is making the "next double" and not the "next double".

  • Now I have denied, the answer besides suggesting as certain an unnecessary concept is mistaken.

Browser other questions tagged

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