Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 5 -Intro to programming javascript

MIT 153 – Assignment #5

This assignment involves making some changes to the program
you created in Assignment #3.

Change the type of Loop

Instead of typing in “END” for the
last name when they are finished the user would like to enter the number of
employees before they start entering the data. Change the program to allow them
to enter the number of employees before the main loop. Use a “for” loop to control the repetitions.

When entering the data the users are sometimes in too much
of a hurry and accidentally enter a name where the hours should go, or a number
where the name should be. We have been asked to include some data validation in
the program.

Create an Input Function

Create a function that be used to enter a text field. It
should accept a string field that it will use to create a prompt. The prompt
should read:

“Please enter ____
(Text required.)

The underlined area shown should contain the string passed
to it as a parameter.

After they enter the data it must be tested to be sure that
they entered text data. If they enter a number or an empty string then an
appropriate message should be displayed and the user given a chance to enter the
correct data.

Change the program to use this function for the input of the
name.

Allow the User to Cancel

If the user clicks the Cancel
button instead of entering a name the program should stop without asking for
any more input.

Create another Input Function

Create a function that will be used to input and validate a
number. It will accept a string that it will use to build a prompt, much like
test entry function above. It will also accept two more parameters that will be
used to specify the range of valid values. The prompt should end up looking
something like this:

“Please enter ____
(Number between __ and __ required.)

Use this function to input the hours worked. The hours
worked should than zero. No employee will ever work more than 60 hours, so use that
as a maximum.

Allow the User to Cancel on Hours

If the user clicks the Cancel button instead of entering the number of hours the program should stop without asking for any more input.

Answer

<html>
<head>
<script type="text/javascript">
// MIT 153-Assiggnment#5
function nameFun(){
            name =prompt("Please Enter Employee Name:","");
			if(name == null){
			return;
             }
			 if (!isNaN(name)){		  	   
		   alert("Error in input - please enter Name again"); 
           name =prompt("Please Enter Employee Name:","");	           		   
		   }
 }
 function hoursFun(){
           hours =prompt("Please Enter Number of Hours Worked:","");
			if(hours == null){
			return;
            }
           if ((hours<0) || (hours>60) || (isNaN(hours))){
			alert("Error \n Please enter Number between 0 and 60");
			hours =prompt("Please Enter Number of Hours Worked:","");
			}
 }
</script>
</head>
<body>
<script type="text/javascript">
// DECLARATION
var name;
var numEmployee;
var hours;
var wage=10;
var totalEarn;
var hoursCount=0;
var totalCount=0;
var BR="<br/>";
var ctr;
// INPUT
numEmployee=prompt("Enter Number of Employee:","");
 //THE for LOOP  
      for(ctr=1;ctr<=numEmployee; ctr++){
	         nameFun(); 
			 if(name == null)
			break;
	       	hoursFun();	
          	if(hours == null)
			break;	
           totalEarn=wage*hours;
		   hoursCount=parseFloat(hours)+parseFloat(hoursCount);
		   totalCount=parseFloat(totalEarn)+parseFloat(totalCount);
		   document.write("Employee Name:  " + name  + BR);
           document.write("Total Earning:  " + totalEarn  + BR+ BR);          	      
	       }
     document.write("Total Hours Worked:  " + hoursCount  + BR); 
     document.write("Total Earnings:  $" + totalCount  + BR);	 
</script>
</body>
</html>



About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!