/    Sign up×
Community /Pin to ProfileBookmark

prompt box calculation problem

I’m new to javascript and am having a problem figuring out an assignment. We are supposed to use 3 prompt boxes where you can enter in your energy-bill amount for the corresponding month (jan, feb, and mar) After this we are supposed to take the responses and put them in an alert box. However, the last part of the alert box is to post the average of the 3 months worth of bills. For example the alert box should look like

january = $100
february = $200
march = $300
average = $200

However, I can’t seem to come up with a way to average the prompt responses. It seems like it should be super easy, but no matter what I do, it doesn’t calculate correctly. Any suggestions would be appreciated.
Thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@noahd1Nov 08.2003 — Josh -

Do you want us to do your homework for you? ?

I'm not sure what you're struggling with here...is it a javascript problem you're having?

Looks like you'll need an HTML form (look up <form> tags), some input boxes (look up <input> tags) and then a javascript function which takes the user's input and produces an alert box.

In the form tag you'll want something like:

<form onSubmit="joshFunction(this)" ... >

where you supply the function...
Copy linkTweet thisAlerts:
@josh_javascriptauthorNov 09.2003 — Noah,

Thanks for the response. Here's the what I came up with for the assignment. This is an intro to programming class, and it's my first experience with javascript. The only part I can't figure out is getting the correct return value after dividing the 3 months worth of bills for an average monthly cost.

<HTML>

<HEAD>

<TITLE> More Programming with Expressions </TITLE>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">

<!-- HIDE FROM INCOMPATIBLE BROWSERS

// Step 6 --- Average monthly energy cost


var december=prompt("Please enter your NSP bill amount for the month of December.");

// Ask user for December bill amount

var january=prompt("Please enter your NSP bill amount for the month of January.");

// Ask user for January bill amount

var february=prompt("Please enter your NSP bill amount for the month of February.");

// Ask user for February bill amount

var returnValue = average_numbers(december, january, february);

function average_numbers(december, january, february) {

var sum_of_numbers = december + january + february;

var result = sum_of_numbers / 3;

return result;

}


alert("December billt" + december + "rJanuary billt" + january + "rFebruary billt" + february + "rAverage billt" + returnValue);


</SCRIPT>

</BODY>

</HTML>
Copy linkTweet thisAlerts:
@noahd1Nov 09.2003 — prompt returns a string. in normal cases, javascript will handle type conversion for you (from string to integer) but since the + operator means both "add two numbers" and "concatenate two strings", you have to explicitly convert the strings to integers before you can do math with them.

the function parseInt() should do the job.

Generally you want to put scripts in the <head></head> section of your document, not in the body. The body is for HTML.

You're also missing a

// -->

before your closing </script> tag. not a big deal, but you have

<!-- HIDE FROM INCOMPATIBLE BROWSERS

but never close it.
Copy linkTweet thisAlerts:
@josh_javascriptauthorNov 09.2003 — Noah,

Thanks much. Your suggestion to use parseInt worked perfectly.

Thanks again,

Josh
×

Success!

Help @josh_javascript spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...