+ 2

Anyone can help me with me Java script code

I have problem for get prime numbers this is my code I want show the max numbers inside the array with his positions (the max number will be prime number and I don't know well how the way for get that numbers ) n1=parseInt(prompt("Insterta los numeros")); n2=parseInt(prompt("Insterta los numeros")); n3=parseInt(prompt("Insterta los numeros")); n4=parseInt(prompt("Insterta los numeros")); n5=parseInt(prompt("Insterta los numeros")); n6=parseInt(prompt("Insterta los numeros")); n7=parseInt(prompt("Insterta los numeros")); n8=parseInt(prompt("Insterta los numeros")); n9=parseInt(prompt("Insterta los numeros")); n10=parseInt(prompt("Insterta los numeros")); var arreglo = [n1,n2,n3,n4,n5,n6,n7,n8,n9,n10]; var mayor = 0; for(i = 0; i < arreglo.length; i++){ if (arreglo[i] % 2 == 0) { mayor = Math.max(arreglo[i]); indice = i; } } document.write("El mayor es "+mayor+" y esta en la posicion "+indice);

3rd Nov 2020, 10:34 PM
Joan Manuel
Joan Manuel - avatar
3 Respuestas
+ 4
It looks like you're asking the user for 10 numbers and how of these, you want to calculate the largest of the prime numbers. In other words, you want to filter numbers that are not prime and out of those results get the maximum. Am I misunderstanding anything? If I'm correct above, the code below should work. There is no need to temporarily store in an Array. You can aggregate the index and max value while reading in numbers from the user. I tried to stick with your Spanish variable names and assume they're for the index and max results. function isPrime(val) { for (var i = 2; i < val; i++) { if (val % i === 0) return false; } return true; } var mayor = 0; var indice; for (var i = 0; i < 10; i++) { var val = parseInt(prompt("Insterta los numeros")); if (isPrime(val)) { mayor = val; indice = i; } } document.write("El mayor es "+mayor+" y esta en la posicion "+indice);
4th Nov 2020, 5:06 AM
Josh Greig
Josh Greig - avatar
+ 1
Sergey, I put effort into answering this question and want Joan to receive that value. I don't want the question and my thoughtful answer deleted just because you prefer seeing a linked code instead of pasting the code directly in the question.
4th Nov 2020, 9:22 AM
Josh Greig
Josh Greig - avatar
0
ok
4th Nov 2020, 9:26 AM
Sergey
Sergey - avatar