<p id ="Prime"></p>
<script>
const lowernumber = parseInt(prompt("Enter the lower number : "));
const highernumber = parseInt(prompt("Enter the higher number : "));
let i;
let j;
let y ="";
for (i=lowernumber;i<=highernumber;i++)
{
let flag =0;
for (j=2;j<i;j++)
{
if (i % j == 0)
{
flag=1;
break;
}
}
if(i>1)
{
if(flag==0)
{
console.log("Prime numbers are :" + i);
}
else
{
y+=i + "</br>";
document.getElementById("prime").innerHTML= "Composite numbers are : " + '</br>' +y;
}
}
}
</script>
I wrote this JavaScript program to find out both Prime and composite Numbers in an interval, also i got the output, but I got the output in two different places like in the console and the browser.
But I want both the outputs to be displayed in the browser only instead of in the console, How i can do that?
2
Answers
Stop use console.log(). Crate a new html element. Then use document.getElementById("element").innerHTML = your prime number
You can do this by creating to p tags.
And like you did show composite numbers on p tag, you can copy this work for prime numbers.
I hope this will help you.