Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Write a function in JavaScript which accept three numbers as arguments and display the greatest number

I tried it using making an array first and then choosing max out of it but the output is always NaN

<!DOCTYPE html>
<html>
<body>

<h2>Write a function in JavaScript which accept three numbers as arguments and
 display the greatest number.</h2>



<p id="demo"></p>

<script>


selnum = [num1, num2, num3];

var num1 = prompt("Please wirte any number");
var num2 = prompt ("Please write 2nd number");
var num3 = prompt("Please wirte 3rd number");

document.write(Math.max(selnum));

</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

There are multiple solutions.

You can do it with an Array, or with only var.

With array:


var num1 = prompt("Please write any number");
var num2 = prompt("Please write 2nd number");
var num3 = prompt("Please write 3rd number");

var arr = [num1, num2, num3];

document.write(Math.max(...arr));

With var:

var num1 = prompt("Please write any number");
var num2 = prompt("Please write 2nd number");
var num3 = prompt("Please write 3rd number");

document.write(Math.max(num1, num2, num3));

If you don't want to you Math.max you can do:

var num1 = prompt("Please write any number");
var num2 = prompt("Please write 2nd number");
var num3 = prompt("Please write 3rd number");

var arr = [num1, num2, num3];

var maxi = num1;

for(let i = 1; i < arr.length; i++){
    maxi = (maxi < arr[i] ? arr[i] : maxi);
}

document.write(maxi);

With a function it will look something like:

function f(num1, num2, num3){
   return Math.max(num1, num2, num3); // Without array
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Found this as the working one

<script>

var num1 = prompt("Please wirte any number");
var num2 = prompt("Please write 2nd number");
var num3 = prompt("Please wirte 3rd number");




document.write(Math.max(num1, num2, num3));


</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your selnum = [num1, num2, num3]; is happening before you actually get the numbers from the user. You need to put that line after the prompt() lines.

You may also need to convert the values to numbers, as prompt() always returns a string and these may not sort the way you expect.

Finally, Math.max doesn't accept an array, it accepts an unlimited number of parameters. So you need to use .apply here.

var num1 = prompt("Please wirte any number");
var num2 = prompt("Please write 2nd number");
var num3 = prompt("Please wirte 3rd number");
    
var selnum = [parseInt(num1, 10), parseInt(num2, 10), parseInt(num3, 10)];
    
console.log(Math.max.apply(Math, selnum));

Also, document.write really isn't used anymore. Use console.log and check your developer console in the browser.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda