Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling a function --stumped

Status
Not open for further replies.

natureboy76

Programmer
Oct 2, 2010
8
US
Can anyone help me with the next step? Total overwhelmed noob..... After a correct code has been entered, call a function using the code to determine the Plan Cost Per Month. The Plan Cost Per Month will be returned to the body. Here is what I have so far....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html xmlns=" xml:lang="en" lang="en">

<head>
<title>

</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-us" />

<script type="text/javascript">
/* <![CDATA[ */
for (var i=1; i<2; i++) {
var ans = parseInt(prompt ("Enter a number - Code 1,2 or 3", ""));
if ((isNaN(ans)) || (ans == "") || (ans <1) || (ans >3)) {
alert ("You must enter a number 1, 2 or 3!! ");
i -- ;
}
}

/* ]]> */
</script>
</head>

<body>

<h1> <br /></h1>

<script type="text/javascript">
document.write("<p><h2>Health Plan</h2></p>");
document.write("<p><h5>.</h5></p>");
</script>

</body>
</html>
Reply With Quote
 
I''m sorry, but the question sounds mysteriously like its homework which we can't help you with due to site policies.


If it isn't perhaps you can rephrase it, as currently it doesn't make much sense.

1. You don't have function so not sure what you want to call.

2. Your for loop is completely pointless. If you are trying to present the user with the prompt until they input a number a while is a much better option with less code.

3. We have no idea what your criteria to determine plan cost per month is.

4. document.write() should not be used in that fashion.
If you need to output to the page use innerHTML properties of an element, rather than overwriting the page.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
The numbers 1, 2, 3 refer to codes for insurance plans.

1 Single Plan $250
2 2 person Plan $350
3 Family Plan $500

I am not looking for someone to complete my work--just point me in the right direction. This is so much more confusing than html lol.
 
You need to create a function if you want to call one.

Perhaps a tutorial may work for you:

Also a read in that tutorial will help you out in managing with JS. Its really not difficult, its different to HTML as HTML is static. JS adds interactivity to websites.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I understand I need to create a function--that's where I am confused. I don't know how to create a function that uses the code to determine the Plan Cost Per Month. Do I make a new variables for each plan code (function planCode( code1, code2,code3){}...Sorry--I am so mad that I can't figure fthis function out. I've read so many tutorials today...Thank you for your knopwledge --it was appreciated.
 
You don't really need to define parameters for it, if all you want to do is show which plan was chosen. Just pass the value you got from your prompt, and use that to determine from the available options.

Code:
<script type="text/javascript">
/* <![CDATA[ */

function Return_Plan(ansNumber){
  if(ansNumber=='1'){
    alert("1 Single Plan $250");
  }
  if(ansNumber=='2'){
    alert("2 person Plan $350 ");
  }

  if(ansNumber=="3"){
   alert("3 Family Plan $500");
  }
}


for (var i=1; i<2; i++) {
var ans = parseInt(prompt ("Enter a number - Code 1,2 or 3", ""));
if ((isNaN(ans)) || (ans == "") || (ans <1) || (ans >3)) {
alert ("You must enter a number 1, 2 or 3!! ");
i -- ;
}
}
Return_Plan(ans);

You need to step back and look at what you want to accomplish, before trying to code it. Most of time if you think step by step, you can program step by step.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you so much. I understand that better now. I'm trying to come up with a variable from the answer given so I can multiply the monthly plan amount by 12 to come upwith the annual cost. This is what I have---it does not seem right at all. I understand it in Java---but JavaScript is confusing.

<body>

<script type="text/javascript">
if (ansNumber ==" 1");
{
document.write("250.00");
}
else (ansNumber == "2");
{
document.write("350.00");
}
else (ansNumber == "3");
{
document.write("500.00");
}

var annualCost = (ansNumber * 12);


</script>
 
Its really not confusing, if you get Java the logic is the same.

Your code is just outputting a number to screen depending on what the value in your ansNumber variable.

Then it takes the value ansNumber and mutliplies it by 12 and assigns the result to the annualCost variable.

Think, if you want to multiply the cost by twelve should you not be assigning that value to a variable and using that to multiply?
Code:
var monthlyCost="";
if (ansNumber =="1");
  {
  monthlyCost=250.00;
 }
...

  var annualCost = (monthlyCost * 12);
This is not Javasript being confusing its regular programming logic that would work in any language from Java to Visual Basic.

Java would do it exactly the same. Assign your value to a variable and use the variable to multiply.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top