I need to write a javascript program that will ask user to enter two numbers. Th
ID: 3801604 • Letter: I
Question
I need to write a javascript program that will ask user to enter two numbers. The program will then find prime numbers between these two numbers inclusively. The output of your program would be a table which contains two columns. The first column would be those prime numbers your program found and the second column contains corresponding square of these prime numbers. For example, if use input 10 and 20, then the prime numbers between these two numbers would be 11, 13, 17, and 19. Then the program will output a table like the following. Note you only need to output at most four smallest prime numbers between these two numbers entered by the user.
11 13 17 19 121 169 289 361Explanation / Answer
<!-- Hope the following Code Solves your Criteria,All The Best-->
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>This is to find prime numbers and their squares between two positive integers inclusively</p>
<input type="text" id="myNumber1" value="">
<input type="text" id="myNumber2" value="">
<button>Compute</button>
<br></br>
<table id="myTable">
<tr>
<td>Prime Number</td>
<td>Its Square</td>
</tr>
</table>
<br>
<script>
function myFunction() {
var x = document.getElementById("myNumber1").value;
x = parseInt(x);
var y = document.getElementById("myNumber2").value;
y=parseInt(y);
var numbers=[];
for(i=x;i<=y;i++)
{
if(testPrime(i))
numbers.push(i);
}
var table = document.getElementById("myTable");
for(i=0;i<numbers.length;i++){
var row = table.insertRow(i+1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = numbers[i];
cell2.innerHTML = Math.pow(numbers[i],2);
}
}
function testPrime(n)
{
if (n===1)
{
return false;
}
else if(n === 2)
{
return true;
}else
{
for(var x = 2; x < n; x++)
{
if(n % x === 0)
{
return false;
}
}
return true;
}
}
</script>
</body>
</html>