skip to Main Content

Code
Output

<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


  1. Stop use console.log(). Crate a new html element. Then use document.getElementById("element").innerHTML = your prime number

    Login or Signup to reply.
  2. You can do this by creating to p tags.

    <p id ="Prime"></p>
    <p id ="Composite"></p>
    

    And like you did show composite numbers on p tag, you can copy this work for prime numbers.

    <p id ="Prime"></p>
    <p id ="Composite"></p>
    
    <script>
    const lowernumber = parseInt(prompt("Enter the lower number : "));
    const highernumber = parseInt(prompt("Enter the higher number : "));
    let i;
    let j; 
    let y = "";
    let z = "";
    
     
    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)
            {
                y + = i + "</br>";
                document.getElementById("Prime").innerHTML= "Prime numbers are : " + '</br>' + y;
            }
    
            else
            {
                z + = i + "</br>";
                document.getElementById("Composite").innerHTML= "Composite numbers are : " + '</br>' + z;
            }
        }
    }
    </script>
    

    I hope this will help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search