Question

Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment:

Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by
(1) Adding one parameter in function header. That parameter is used to accept a number as the argument. This function is used to find out whether a number passed into this function is prime or not.
(2) Adding a for or while loop to check whether that number is only divisible by itself and 1. If not, then return false;
(3) After the loop, return true, indicates the number is a prime.

Step 2. [30 points] In “find_primeV2.js”, complete getPrimes() function by
(4) Adding one parameter in function header. That parameter is used to accept user inputted number as the argument. This function is used to find all primes between 2 and the number that user enters.
(5) Adding a for or while loop to check each integer between 2 and the number that user enters. In each loop iteration, call isPrime() function to determine whether the integer is a prime or not. If the integer is a prime number, then add that integer to primes string, and increase count by 1.
(6) After the loop, at the end of the function, return an array that holds both primes and count.

Step 3. [20 points] In “find_primeV2.js”, complete processEntries() function by adding js statements:
(1) In the if statement block, display a message says: “Please enter an integer number greater than 1.” in the <span> element next to the number input box.
(2) In the else statement block, remove error message if have any.
(3) Add js code to call getPrimes() function to get the number of primes found in the range of 2 to the inputted number and display that result in the input box with id=”count” in the web page.
(4) Add js code to call getPrimes() function to get the list of primes found in the range of 2 to the inputted number and display those primes in the text area with id = “primes”.

find_primeV2.js

// $ function is used to replace document.getElementById() in this document for convience
var $ = function(id) { return document.getElementById(id); };

// determine if a number is prime
var isPrime = function( ) {//step 1.1 add a parameter in function header to accept
// a number passed in
//step 1.2 add a for or while loop to check whether that number is prime or not
// if that number can be divisible by any integer between 2 and (number itself - 1)
// then that number is not a prime, return false

//step 1.3: after loop, return true, indicates that number is a prime


}

// get all prime numbers
var getPrimes = function (){ //step 2.1 add a parameter in function header
  
   var primes = "";
   var count = 0;
   //step2.2: add a for or while loop
   // inside the loop, call isPrime() function
   // inside the loop, add prime number to primes string and update the count

   //step 2.3: return an array that holds both primes string and count
  
}
var processEntries = function() {
//get values from page
var input = $("number").value;
   input = Number(input);
inputInteger = parseInt(input);
   $("primes").value = "";
  
   // add data validation here, to make sure the input is a number greater than 1
   if ( isNaN(input) || input!== inputInteger || inputInteger <= 1){
   //step 3.1: add js code to display a message says: "Please enter an integer number greater than 1."
   //besides the input entry box
      
      
       $("count").value = "";
       $("primes").value ="";
       $("primes").style = "background-color: #808080";
       $("count").style = "background-color: #808080";
   }
   else {
   //step 3.2: add js code to remove error message if having one
         
   $("primes").style = "background-color: #ccffff";
   $("count").style = "background-color:#ccffff";
  
   //step 3.3: add js code to call getPrimes() function to display number of primes found in the range
// in the input box with id = "count"  
  
   //step 3.4: add js code to call getPrimes() function to display the list of primes found in the textarea
   // with id = "primes"
  
   }
}
   $("calculate").onclick = processEntries;
   $("number").focus();

find_primeV2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">  
<title>Future Value Calculator</title>
<link rel="stylesheet" href="prime.css">

</head>

<body>
<main>
<h1>Find Prime Numbers</h1>
       <img src="images/teacher.png" id="teacher" alt="teacher" width="177" height="277"/>
<p>Enter any a number to find out how many prime numbers there are
up to and including that value.</p>
         
<label for="number">Enter Number:</label>
<input type="number" id="number" value="100">
<span>&nbsp;</span><br>
      
<label for="count">Prime count:</label>
<input id="count" disabled><br>
  
<label for="primes">Prime numbers:</label>
<textarea id="primes" rows = "10" cols= "40" disabled></textarea><br>
  
<label>&nbsp;</label>
<input type="button" id="calculate" value="Calculate"><br>
</main>
<script src="find_primeV2.js"></script>
</body>
</html>

prime.css

/*@import url(http://fonts.googleapis.com/css?family=Wellfleet);*/
body {
font-family: 'Wellfleet', Arial, Helvetica, sans-serif;
background-color: white;
margin: 0 auto;
width: 800px;
padding: 0 1em .5em;
}
h1 {
   color: blue;
   margin: .5em 0;
}
#teacher {
   float:right;
   margin:0px 30px 0px 0px;}

  
label {
   float: left;
width: 10em;
text-align: right;
padding-bottom: .5em;
}
input {
width: 5em;
   margin-left: 1em;
margin-bottom: .5em;
}

textarea {
   width: auto;
   height: auto;
   margin-left: 1em;
margin-bottom: .5em;
}
span {
color: red;
font-size: 80%;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code updated in Javascript no changes in HTML CSS

find_primeV2.js

// $ function is used to replace document.getElementById() in this document for convience
var $ = function(id) { return document.getElementById(id); };

// determine if a number is prime
var isPrime = function( num ) {//step 1.1 add a parameter in function header to accept
// a number passed in
//step 1.2 add a for or while loop to check whether that number is prime or not
// if that number can be divisible by any integer between 2 and (number itself - 1)
// then that number is not a prime, return false

for(var i = 2; i < num; i++)
if(num % i === 0) return false;
return true;

//step 1.3: after loop, return true, indicates that number is a prime

}

// get all prime numbers
var getPrimes = function ( num ){ //step 2.1 add a parameter in function header
var arr = [];
var primes = "";
var count = 0;
//step2.2: add a for or while loop
// inside the loop, call isPrime() function
// inside the loop, add prime number to primes string and update the count

for(var i = 2; i <= num; i++){
   if( isPrime(i) ) {
       count++;
       primes += i.toString() + " "
   }  
}
   arr.push( count );
   arr.push( primes );
   console.log(arr);
//step 2.3: return an array that holds both primes string and count
   return arr;
}
var processEntries = function() {
//get values from page
var input = $("number").value;
input = Number(input);
inputInteger = parseInt(input);
$("primes").value = "";
  
// add data validation here, to make sure the input is a number greater than 1
if ( isNaN(input) || input!== inputInteger || inputInteger <= 1){
//step 3.1: add js code to display a message says: "Please enter an integer number greater than 1."
//besides the input entry box
alert("Please enter an integer number greater than 1 ");
  
$("count").value = "";
$("primes").value ="";
$("primes").style = "background-color: #808080";
$("count").style = "background-color: #808080";
}
else {
//step 3.2: add js code to remove error message if having one

$("primes").style = "background-color: #ccffff";
$("count").style = "background-color:#ccffff";
  
   arr = getPrimes( inputInteger );
   console.log(inputInteger);
//step 3.3: add js code to call getPrimes() function to display number of primes found in the range
// in the input box with id = "count"
   $("count").value = arr[0];
  
//step 3.4: add js code to call getPrimes() function to display the list of primes found in the textarea
// with id = "primes"
   $("primes").value = arr[1];
   console.log(arr[1]);
}
}
$("calculate").onclick = processEntries;
$("number").focus();



find_primeV2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Future Value Calculator</title>
<link rel="stylesheet" href="prime.css">

</head>

<body>
<main>
<h1>Find Prime Numbers</h1>
<img src="images/teacher.png" id="teacher" width="177" height="277"/>
<p>Enter any a number to find out how many prime numbers there are
up to and including that value.</p>

<label for="number">Enter Number:</label>
<input type="number" id="number" value="100">
<span>&nbsp;</span><br>
  
<label for="count">Prime count:</label>
<input id="count" disabled><br>
  
<label for="primes">Prime numbers:</label>
<textarea id="primes" rows = "10" cols= "40" disabled></textarea><br>
  
<label>&nbsp;</label>
<input type="button" id="calculate" value="Calculate"><br>
</main>
<script src="find_primeV2.js"></script>
</body>
</html>


prime.css

/*@import url(http://fonts.googleapis.com/css?family=Wellfleet);*/
body {
font-family: 'Wellfleet', Arial, Helvetica, sans-serif;
background-color: white;
margin: 0 auto;
width: 800px;
padding: 0 1em .5em;
}
h1 {
color: blue;
margin: .5em 0;
}
#teacher {
float:right;
margin:0px 30px 0px 0px;}

  
label {
float: left;
width: 10em;
text-align: right;
padding-bottom: .5em;
}
input {
width: 5em;
margin-left: 1em;
margin-bottom: .5em;
}

textarea {
width: auto;
height: auto;
margin-left: 1em;
margin-bottom: .5em;
}
span {
color: red;
font-size: 80%;
}

Sample Output

Add a comment
Know the answer?
Add Answer to:
Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Develop the Change Calculator application In this exercise, you’ll create an application that displays the minimum...

    Develop the Change Calculator application In this exercise, you’ll create an application that displays the minimum number of quarters, dimes, nickels, and pennies that make up the number of cents specified by the user. Without the use of a JavaScript Library (for coins). 1.      Open the HTML and JavaScript files below: 2.      In the JavaScript file, note that three functions are supplied. The $ function. The start of a calculateChange function. And an onload event handler that attaches the calculateChange...

  • Modify an application that lets users add tasks to a list so the tasks can also...

    Modify an application that lets users add tasks to a list so the tasks can also be deleted when they’re completed. 1. In the HTML file, enclose the text for each of the three existing list items in a <p> element. Then, Add buttons like the ones shown above preceding the <p> elements. No ids or names are required for these buttons, but they should be assigned to the class named “delete”. 2. In the JavaScript file, modify the event...

  • in the following java script code follow the instructions provided <!DOCTYPE html> <html> <head> <!-- JavaScript...

    in the following java script code follow the instructions provided <!DOCTYPE html> <html> <head> <!-- JavaScript 6th Edition Chapter 5 Hands-on Project 5-2 Author: Date:    Filename: index.htm --> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hands-on Project 5-2</title> <link rel="stylesheet" href="styles.css" /> <script src="modernizr.custom.05819.js"></script> </head> <body> <header> <h1> Hands-on Project 5-2 </h1> </header> <article> <h2>Change of address form</h2> <form> <fieldset id="contactinfo"> <label for="addrinput"> Street Address </label> <input type="text" id="addrinput" name="Address" /> <label for="cityinput"> City </label> <input type="text" id="cityinput" name="City"...

  • In this exercise, you’ll upgrade a version of the MPG application so the error messages are...

    In this exercise, you’ll upgrade a version of the MPG application so the error messages are displayed in span elements to the right of the text boxes. Open the HTML and JavaScript files in this folder: exercises_short\ch06\mpg\ Then, run the application and click the Calculate MPG button to see that an error message is displayed in an alert dialog box for each of the two input fields. 2. In the HTML file, add a span element after the input element...

  • In an external JavaScript file, you need to create two global variables for the Quarter name...

    In an external JavaScript file, you need to create two global variables for the Quarter name array and the Sales amount array. Create an anonymous function and connect it to the onclick event of the Add to Array button that adds the values from the fields (Quarter and Sales) to the respective arrays. Create an anonymous function and connect it to the onclick event of Display Sales button displays the contents of the arrays, displays the sum of all the...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Java Write a Simple Program This question is about following the directions below. Failure to do...

    Java Write a Simple Program This question is about following the directions below. Failure to do so, results in lost credit. Write the definition of a method that takes in an integer number, and returns true if the number is prime, otherwise, returns false. A prime number is only divisible by itself and 1. Do not write the code for main here. The code must follow these steps in the order shown: 1. Name the method isPrime, and make it...

  • finished the web page ********************************************************* .js // I've added a span with the id "count" which...

    finished the web page ********************************************************* .js // I've added a span with the id "count" which you can use to modify // the number of clicks on the page // you can do this by getting the value; incrementing it and then // modifying the value of the span function changeColor(){     const colors = ['red', 'green', 'blue', 'cyan', 'yellow', 'black', 'silver', 'tan'];     // Choose a random float from [0, 1) the multiply by length to get random index     // Math.floor()...

  • I need to complete 3 validation cases in this Javascript code. I need to validate email,...

    I need to complete 3 validation cases in this Javascript code. I need to validate email, phone and street address. How would I go about validating these 3 cases in this code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Week 10: Regular Expressions</title> <style type="text/css"> span { padding: 2px; } .success { color: #008000; background: #99cc99; } .failure { color: #ff0000; background: #ff9999; } </style> <script type="text/javascript"> function validateInput(form) { var result0 = document.getElementById('result0'), result1 = document.getElementById('result1'), result2 = document.getElementById('result2'),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT