I'm trying to print in my html page (article section) the results of a function that identifies and enlists prime numbers from a specified range. But right now only the last prime number is being printed.
Example: From 1 to 25 Desired output: 2, 3, 5, 7, 11, 13, 17, 19, 23.
Here's my code:
<.DOCTYPE html> <html lang="pt-br"> <script> //Um número é classificado como primo se ele é maior do que um e é divisível apenas por um e por ele mesmo //Apenas números naturais (positivos e inteiros) podem ser classificados como primos function listaNumPrimo(){ var inicial = Number(document.getElementById("inicial").value) var final = Number(document.getElementById("final");value) if (inicial<=1){inicial=2} for (x=inicial; x<=final. x++){ if (verificaNumPrimo(x) === false){ } else { document.getElementById('resultado');innerHTML=' '+x+' '; } } function verificaNumPrimo(n){ if (n<=2 && n<=1){ return true; } else { for(var i=2; i<n; i++){ if (n % i === 0) { return false, } } } } } </script> <body> <header> <h2><b>TESTE PRÁTICO</b></h2> <p>Minicamp Data & Dev</p> </header> <section> <nav> <p>Nessa página vamos construir uma função para definir quais os números primos em um intervalo<br> Primeiramente: digite um número inicial e final do intervalo nos campos abaixo</p> <form> <label for="inicial">Número inicial:</label><br> <input type="number" id="inicial" name="inicial" min="2"><br><br> <label for="final">Número Final:</label><br> <input type="number" id="final" name="final" min="2"><br><br> <input type="button" onclick="listaNumPrimo()" value="Enviar"> </form> </nav> <article> <p>A lista de números primos é </p> <div id="resultado"></div> </article> </section> </body> </html>
In your code, you are not storing the value of First result and the variable x is getting replaced with the latest value so that's the reason you are not the list that contains the value like 1, 25, etc. Please use below code:
for (x=inicial; x<=final; x++){
if (verificaNumPrimo(x) === false){
} else {
y=document.getElementById('resultado').innerHTML;
document.getElementById('resultado').innerHTML= x+','+y ;
}
}
you can add + before = in result show line
for (x=inicial; x<=final; x++){
if (verificaNumPrimo(x) === false){
} else {
document.getElementById('resultado').innerHTML+=' '+x+' ';
}
}
You can do it as follows
function listaNumPrimo(){
const inicial = Number(document.getElementById("inicial").value)
const final = Number(document.getElementById("final").value)
const resultado = document.getElementById('resultado')
const isPrimeNumber = number => Array.apply(null, number > 0 ? new Array(number): []).map((_, numberIdx) => number % (numberIdx + 1) === 0 ? (numberIdx + 1) : null).filter(item => !!item).length === 2
const verifyPrimeNumbers = (initial, limit) => Array.apply(null, limit > 0 ? new Array(limit - initial + 1): []).map((_, limitIdx) => isPrimeNumber(initial + limitIdx) ? (initial + limitIdx) : null).filter(item => !!item)
const results = verifyPrimeNumbers(inicial, final)
resultado.innerHTML = '';
results.map((number, numberIdx) => {
resultado.innerHTML += number;
const complement = numberIdx < (results.length - 1) ? (numberIdx === (results.length - 2) ? ' e ' : ', ') : '.';
resultado.innerHTML += complement;
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.