4 Replies - 689 Views - Last Post: 12 April 2013 - 07:15 AM

#1 domino554  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 13

Determine if number, entered by the user at the keyboard, is a prime

Posted 11 April 2013 - 08:34 PM

I am getting stuck with the "meat and potatoes" Steps: E-F of this script. I have the .html down pat, I would like some help with this javascript. I'm a total newbie if you couldn't already tell.


// Written by: DOMINO554
// Javascript Branching
// Filename: project5.js
// Last revised: 3/24/2013



//A. Start.
// Script: "project5.js"

UI = window.prompt ( "Enter a number to test as a prime number:");
         TV = parseInt(UI,10);


/*B. Display a prompt dialog box and store the user's input in UI.
 	  B1. Store UI acquired from a prompt dialog box.
 	  B2. Parse UI as a Base-10 integer and assign it to variable TV. */



         var	UI;	   	// User input of number to test (string)
         var	TV;		// Test value acquired from user (integer)
		 var	DD;		// Division denominator (integer)
		 var	HITS;   // Quantity of successful integer divisions (integer)
		 
//C. Initialize the HITS counter to zero.
 
 	 	 HITS = 0;
 
//D. Initialize the DD variable to the value of TV.

 //DD = TV/2;

/*E. While DD is greater than zero, repeat the following steps:
 E1. Test if the TV is evenly divisible by DD (that is if the remainder of TV/DD equals zero).
   If so, increment HITS by 1.		 		 
 E2. Decrement DD by 1.*/ 		 
 
 while (UI>0)
 {
 TV%=DD;
 document.write(+ TV + " Test Output of TV ");
 HITS++;
 break;
 }
 
		 
/*F. Display results within an HTML paragraph the browser's document window as seen in the Sample OUTPUT.
 F1. The string UI.
 F2. The string " is"
 F3. If HITS is greater than 2, then display the string " not"
 F4. The string " a prime number." */
  
  
//G. End. 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		/* 
		 //while (DD>0);
		 // {
		 //TV/DD;
		 //HITS++;
		 //HITS--;
		 
		 
		// document.write( + UI + " is not a prime number" );
		// HITS++;
		// }
		 
		// }
		
		 
		
		 
      	 
	 //var	sqnum = numA * numA;	// Square of numA
	 
	

         
		 // To round sqnum to 2 places use the following expression:
	//	 sqnum = Math.round(sqnum*100) / 100;
	
	*/

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Determine if number, entered by the user at the keyboard, is a prime

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,060
  • Joined: 08-June 10

Re: Determine if number, entered by the user at the keyboard, is a prime

Posted 11 April 2013 - 10:57 PM

Assignment said:

[...] and will produce XHTML output in the web page using Java​Script document.write methods

lol. big teacher fail, XHTML explicitly forbids the use of document.write (XML structure violation).
Was This Post Helpful? 2
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,060
  • Joined: 08-June 10

Re: Determine if number, entered by the user at the keyboard, is a prime

Posted 11 April 2013 - 11:32 PM

ok, back to topic.

the loop does not make sense. what it currently does is:
- set TV to the modulus of TV and DD
- print something out
- increase HITS
- loop forever (in a modulus calculation, the remainder (modulus) is always less that the denominator. if you calculate that remainder again with the same denominator, you must--inevitably--get the same result, which leads to an infinite loop)

if you intend not to use any of the prime factoring strategies (e.g the Sieve of Eratosthenes), you have pretty much to calculate. the most basic approach is more like
- calculate modulus of UI (a number, needs to be converted) and TV (usually starting at 2)
- test if the modulus is 0
- increase TV
- loop until TV hits its limit (that is at latest the UI)


PS. XHTML files end with .xhtml, not .html.

PPS. you can only allow up to 15 digits in the input, otherwise you will fall victim to the implementation of Floats (the only number type in Java​Script (to be more precise: IEEE 754 64bit Double Floating Point Number)).

This post has been edited by Dormilich: 11 April 2013 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Determine if number, entered by the user at the keyboard, is a prime

Posted 12 April 2013 - 06:21 AM

Quote

lol. big teacher fail, XHTML explicitly forbids the use of document.write (XML structure violation).


Yeah, you might want to point this out to your professor.

Extra, additional fail: refers students to W3Schools: http://gibson.vero-b.../178422sch.html

I need to pick up a job as a professor I think. For extra cash.
Was This Post Helpful? 1
  • +
  • -

#5 domino554  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 13

Re: Determine if number, entered by the user at the keyboard, is a prime

Posted 12 April 2013 - 07:15 AM

Thanks for the reply's. I will analyze and try to interpret your suggestions.

I greatly appreciate the quick response.

Note: sent professor info on document.write.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1