|
-
May 13th, 2007, 07:41 AM
#1
Help!
I've just started doing Java, and as usual, everything is going over my head at this point of time, can someone please help me with these codes, please?
I'll most greatful.
A JavaScript program is intended to accept two numbers from a user,
then write the sum and difference of the two numbers to the browser
window. For example, if the user inputs36 and 24 the program should
write
The sum of 36 and 24 is 60
The difference of 36 and 24 is 12
However, the current version (Figure6) contains several errors. Your
task is to identify them, correct the program, and write a short
explanation of each of your changes.
<SCRIPT LANGUAGE="JavaScript">
// A program to find the sum and difference of two numbers
var firstNum;
var secondNum;
firstNum = window.prompt('Please enter the first number
′′
, '');
firstNum = ParseFloat(firstNum);
secondNum = window.prompt('Please enter the second number', '');
secondNum = parseFloat(secondNum);
document.write'The sum of ' + first + ' and ' + secondNum + ' is
' + (firstNum + secondNum) + '<BR>');
document.write('The difference of ' firstNum + ' and ' +
secondNum + ' is ' + (firstNum + secondNum) + <BR>);
</SCRIPT>
Figure 6 A JavaScript program containing errors
Q4
(a)Identify as many errors as you can, then rewrite the program
correcting the errors, and paste the corrected version into the
document containing your solution. Your tutor will find it helpful
if you highlight where you have made the changes. [6]
(b)For each different error you found write a very brief explanation
of what was wrong in the original code and what change you
needed to make. [6]
(ii) State whether each of the following evaluates to true or false in
JavaScript.
(a)7 >=11 – 2 * 2;
(b)'pluto' > 'jupiter';
(c) '66' == 66;
(iii)An imaginary internet clothing store operates the following delivery
charges.
•
For orders worth less than £25 the delivery charge is £3.00.
•
Orders worth at least £25 but less than £100 are delivered free.
•
Orders worth £100 or more are delivered free and in addition the
customer receives a free gift.
We have started writing a JavaScript program that prompts a user to
enter the value of their order, then tells them which of the categories
above will apply.
// A delivery charge program
var orderValue; // How much the order is worth
orderValue = window.prompt('How much is the order worth in
Pounds Sterling?','');
document.write('The value of your order is £' + orderValue
+ '.<BR>');
// Insert your code here
document.write('<BR>');
document.write('Thank you for shopping with us.');
(a)Using program_7.3.9 as a guide, complete the code above so that
it will work out which band the order falls in,then display a
suitable message on the screen using the document.write()
method. Please note the following points.
1 You will need to add appropriate HTML tags before and after
the code, as well as completing the JavaScript where
indicated. program_7.3.9will show you what HTML tags to
use. You should give the window the title 'Delivery charges'.
2 There is no need for your program to check that the user's
input is actually a valid number; you may assume that this will
always be the case.
3 You should use indentation to help make the structure of your
program clear, in the same way program_7.3.9does.
4 A copy of the incomplete program as shown above in Figure7
can be found in the file Q4(iii).html that you downloaded
with this assignment.
Insert a copy of your completed program into the document
containing your solution. [8]
(b)Run your program with each of the following inputs. (Note that
you only need to enter the figures, without any £ sign.)
• 24
• 25
• 99
• 100
For each run insert a screen shot of the browser window into the
document containing your solution.
Please, please, please, I need to submit it on May 15th.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|