GeniusAnswers.com
 
Not a member?

LIVE SUPPORT

Environmental Studies Homework

Computer Science

Question (10 Credits)

I need computer science homework help:

I have uploaded a question file showing a program that is intended to find the average of two numbers. The user will enter two numbers (say 5 and 7) and the program will write the answer to the screen (in this case 6).
However the current version in the uploaded question file, contains several errors. Your task will be to identify them, explain what is wrong, and say what the correct code should be.
When you have identified the errors, rewrite the program correcting the errors.
For each distinct error that you identified, write down separately a short explanation of what you changed and why you made the changes.

<SCRIPT LANGUAGE="JavaScript">
//To find the average of two numbers
var 1srValue,2ndValue,answer;
1stValue=window.prompt('Enter number 1',");
1stValue=parseFloat(1stValue);
2ndValue=window.prompt(Enter number 2',");
2ndValue=parseFloat(2ndValue);
result=1stValue + 2ndValu/2;
document.Write'The average of' 1stValue and
2ndValue 'is' answer);

Answer

Hello! I'd be happy to help you with these computer science homework answers and any future assignments. Normally I try to work you through the problem, but with things such as computer science language syntax, sometimes the only method is simply pointing out the right code.

I'm guessing you are going over a few different languages, seeing as he threw in some things from what looks like C++ or java. I'll give you the basics here, allow you to look for the errors in the original problem, then give you the full solution.

Keep these things in mind:
-semicolons are not needed with javascript in HTML
-variable names must start with letters or underscores (_), and never numbers
-displayed text is always noted with double quotation marks (")
-combining text and variables should be done with the plus (+) operator between each switch
-variables must be declared and kept consistent
-Remember proper notation of operations is multiplication (*) and division ( / ) first, then addition (+) and subtraction (-). If you need to clarify, always use parenthesis ()
-built in function names are case sensitive
-function components should be contained within parenthesis ()
-don't forget to close your script with the appropriate tag, else the HTML document in which it resides will think all of the HTML is also part of the javascipt, and then nothing will be displayed

And on an aesthetic note (non-coding tweak):
-be careful with spacing when combining text and variables. You want lines to read:
"The average of 5 and 7 is 6" NOT "The average of5and7is6" 

Try to look through the work now to see if you can spot what needs to be fixed. When you're done, compare with mine (comments are provided to help you link my hints above to where they apply)

<SCRIPT LANGUAGE="JavaScript">
// To find the average of two numbers
// unlike java or c++, semicolons are not needed at the ends of lines

var _1stValue, _2ndValue, answer
// variables must start with letters or underscores
// be sure to change these everywhere either to _1stValue or FirstValue

_1stValue=window.prompt("Enter number 1", "")
// all displayed text must be surrounded in double quotes
_1stValue=parseFloat(_1stValue)

_2ndValue=window.prompt("Enter number 2", "")
// added " before Enter, and switched single quotes to double as well
_2ndValue=parseFloat(_2ndValue)

answer = (_1stValue + _2ndValue) / 2
// the variable result was never defined, but 'answer' was - so use that
// remember order of opperations
// parenthesis are needed to clarify what you want the computer to perform first

document.write("The average of " + _1stValue + " and " + _2ndValue + " is " + answer);
// Write --> write (things are case sensitive)
// Add () after write as it is a function
// double quotes for text (don't forget spacing)
// Add + to combine text with variables

</script>
// must close script 

 Students: Click here to sign up 

sa
© Copyright 2006 Genius Answers.com All Rights Reserved. All text, graphics, images, and links are property of Technovate Scholastic (a division of Technovate Inc.). Technovate Scholastic offers legitimate tutoring by answering academic questions in over 60 subjects (including but not exclusive to Math, Science, Economics). Under no circumstances will Technovate Scholastic write a student's essay. Our posting system offers every student an affordable method of receiving the necessary academic help to succeed in their studies.