Solved

Converting simple VB to JScript

Posted on 1998-09-22
11
177 Views
Last Modified: 2013-12-25
Hello I have the following form in a VB Program

Total CP:  <Textbox name="txtCP">
Total XP:  <Textbox name="txtXP">
Silver:    <Textbox name="txtSilver">
<CommandButton name="cmdCalculate">
Earned CP: <Textbox name="txtCPGot">
Earned XP: <Textbox name="txtXPGot">

What I want to do is recreate this form and it's functionality in an HTML Page.  I've created the form with onclick="CPCalculate()" for the command button.
What I need is the following Visual Basic code converted into JavaScript code that will work on IE4 and Netscape4(if possible).  This form will not make a round trip to the server just Calculate the answer on the client side.

Well here is the VB code for CPCalculate

Private Function FigureCPCost(ByVal Level as Integer, ByVal Which as Integer) as Integer
  Dim Modifier as Integer
 
  Select Case Level
    Case 1: Modifier = 5
    Case 2: Modifier = 10
  end select

  FigureCPCost = Modifier * Which
end function


Private sub CPCalculate()
  Dim Total, CPCount, Level, Extra, Which
  Dim CP, XP, Silver

  if txtCP.text="" then txtCP.text = "0"
  if txtXP.text="" then txtXP.text = "0"
  if txtSilver.text="" then txtSilver.text = "0"

  CP = txtCP.text
  XP = txtXP.text
  Silver = txtSilver.text
  CPCount = 0
  Total = CP + XP + Silver
  Level = ((CP - 25) \ 10) + 1   'notice the \ for integer division
  Extra = (CP - 25) mod 10
  Which = 1

  do while Total > FigureCPCost(Level, Which)
    Total = Total - FigureCPCost(Level, Which)
    CPCount = CPCount + 1
    Which = Which + 1
    if Extra + Which > 10 then
      Level = Level + 1
      Extra = 0
    end if
  Loop

  txtCPGot = CPCount
  txtXPGot = Total
end sub
0
Comment
Question by:Jagar
  • 6
  • 5
11 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 1855215
Here you are:

<SCRIPT LANGUAGE="JavaScript"><!-- // cloak

function FigureCPCost(Level,theWhich) { // Which is a reserved word
   var Modifier=1;
   if (Level==1)      Modifier = 5;
   else if (Level==2) Modifier = 10;

/*   switch could be used but that is JS1.2:
   switch(Level) {
      case 1:  Modifier =  5; break;
      case 2:  Modifier = 10; break;
      default: Modifier =  1; break;
   }
*/
   return Modifier * theWhich;
} // end function

function CPCalculate(theForm) {
      var Total, CPCount, Level, Extra, theWhich, CP, XP, Silver;
      if (theForm.txtCP.value=='')     theForm.txtCP.value     = 0;
      if (theForm.txtXP.value=='')     theForm.txtXP.value     = 0;
      if (theForm.txtSilver.value=='') theForm.txtSilver.value = 0;

      CP      = theForm.txtCP.value;
      XP      = theForm.txtXP.value;
      Silver  = theForm.txtSilver.value;
      CPCount = 0;
      Total   = CP + XP + Silver;

      Level   = (parseInt((CP - 25) / 10)) + 1   // notice the / + parseInt for integer division
      Extra   = (CP - 25) % 10
      theWhich   = 1

      while (Total > FigureCPCost(Level, theWhich)) {
        Total = Total - FigureCPCost(Level, theWhich);
        CPCount = CPCount + 1;
        theWhich = theWhich + 1
        if (Extra + theWhich > 10) {
          Level++;
          Extra = 0
        }
      }

      theForm.txtCPGot.value = CPCount;  
      theForm.txtXPGot.value = Total;
}
// uncloak --></SCRIPT>

<FORM NAME="CPForm">
<BR>Total CP:  <INPUT TYPE="TEXT" NAME="txtCP">
<BR>Total XP:  <INPUT TYPE="TEXT" NAME="txtXP">
<BR>Silver:    <INPUT TYPE="TEXT" NAME="txtSilver">
<BR><INPUT TYPE="BUTTON" VALUE="Calculate" NAME="cmdCalculate" onClick="CPCalculate(this.form)">
<BR>Earned CP: <INPUT TYPE="TEXT" NAME="txtCPGot">
<BR>Earned XP: <INPUT TYPE="TEXT" NAME="txtXPGot">
</FORM>

Enjoy,

Michel
0
 
LVL 2

Author Comment

by:Jagar
ID: 1855216
Thanks later today I should be able to put this on a page and test it, but it looks all right to me.  Don't really know Javascript, but it's basically the same just different syntax on how to do stuff.
0
 
LVL 2

Author Comment

by:Jagar
ID: 1855217
Alright I'm getting a syntax error missing ';' in line 36 char 47.

The error seems to be in the line

While(Total > FigureCPCost(Level, theWhich)){

The 47 Character is after the FigureCPCost call, but if I put a ; there then I get a missing ')' syntax error for the previous character same line.  This is IE haven't tried it in Netscape yet.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1855218
I do seem to have missed some sem-colons:
Level   = (parseInt((CP - 25) / 10)) + 1   // notice the / + parseInt for integer division
Extra   = (CP - 25) % 10
theWhich   = 1

should be

Level   = (parseInt((CP - 25) / 10)) + 1;  // notice the / + parseInt for integer division
Extra   = (CP - 25) % 10;
theWhich   = 1;

Missing semicolons can give errors further down the script

The version I posted ran in MSIE 4.0 here without the change so I hope this helps anyway.

Please be advised that I have not looked at the logic of your script so it should be the same as it was. However if I run it with 4 digit numbers, it takes several seconds to calculate in both ie4 and ns4 so perhaps a warning to the user is in order and perhaps a check that he input is not to big?

Michel

0
 
LVL 2

Author Comment

by:Jagar
ID: 1855219
Thanks I'll check this tomorrow at work.
That's not a problem with large number the absolute largest number would be two to thee digits.
Thank You for your help and like I said I'll check this tomorrow at work and then grade your answer.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:Jagar
ID: 1855220
I must be missing something the following code is giving me the error missing ';' line 34 char 47

<script language="JavaScript"><!--//cloak
function FigureCPCost(Level, theWhich) {
  var Modifier=1;
  if (level==1)      Modifier = 5;
  else if (level==2) Modifier = 10;

  return Modifier * theWhich
}

function CPCalculate(theForm){
  var Total, CPCount, Level, Extra, theWhich, CP, XP, Silver;

  if (theForm.txtCP.value=="")  theForm.txtCP.value=0;
  if (theForm.txtXP.value=="")  theForm.txtXP.value=0;
  if (theForm.txtSilver.value="") theForm.txtSilver.value=0;

  CP = theForm.txtCP.value;
  XP = theForm.txtXP.value;
  Silver = theForm.txtSilver.value;
  CPCount=0;
  Total = CP + XP + Silver;

  Level = (parseInt((CP - 25)/10)) + 1;
  Extra = (CP - 25) % 10;
  theWhich = 1;
 
  While(Total > FigureCPCost(Level, theWhich)){
   Total = Total - FigureCPCost(Level, theWhich);'this is line 34
    CPCount = CPCount + 1;
    theWhich = theWhich + 1;
    if (Extra + theWhich > 10){
      level++;
      Extra = 0;
    }
  }

  theForm.txtCPGot.value = CPCount;
  theForm.txtXPGot.value = Total
}
//uncloak --></script>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1855221
While should be while and // are javascript comments
I cannot see anything wrong and I get no errors in my version - are you sure there are no funny characters that sneaked in when pasting (and why is the While with upper case W????),

Sorry! Please give me a URL where I can try your version and I will look at it at work on Monday...

Michel
0
 
LVL 2

Author Comment

by:Jagar
ID: 1855222
I have one more request.  What I need to do is check print a msgbox during the loop.  Something is not calculating right.  In VBScript I would do
alert CPCount
what would I do in Javascript.  I tried
Alert CPCount;
but got and error.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1855223
alert(CPCount);

Do you still get the other error?

Michel
0
 
LVL 2

Author Comment

by:Jagar
ID: 1855224
No  The error was in the While and not while.
I actually had fixed all the missing ; as I typed.  That's why I could not figure out what the error was.

Thank you for your help.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1855225
Anytime...

Michel

PS: The JavaScript manuals can be gotten at
http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 91
GPS save in database 19 57
Compare site how does it work 4 71
How to automatically insert items to sell into Ebay.com 2 24
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now