Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Prompt scripted

    I'm trying to make a calculator with prompts. I want it to give several different answers. Like one text area will display one answer (example a*b*c) and another text area will display a different answer (example a+b+c). From tutorials I know that to calculate the answer with prompts it would look like this:
    return (a*b*c); break;

    That would be only one of the equations. How would I get both of them and display them in two different text areas?

  • #2
    New Coder
    Join Date
    Dec 2004
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you might understand better if I showed the source I have right now:

    Code:
    <HEAD>
    
    <SCRIPT LANGUAGE="JavaScript">
    
    
    
    
    <!-- Begin
    function gearCalc1(num) {
    switch(num) {
    case 0 : return (""); break;
    case 1 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 2 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 3 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 4 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 5 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
     case 6 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    
    }
    }
    function gearCalc2(num) {
    switch(num) {
    case 0 : return (""); break;
    case 1 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter+fDiameter+rbDiameter+rpm+gaWidth+gfWidth); break;
    case 2 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 3 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 4 :  var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    case 5 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
     case 6 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
             return (pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth); break;
    
    }
    }
    //  End -->
    </script>
    </HEAD>
    
    <!-- STEP TWO: Copy this code into the BODY of your HTML document  -->
    
    <BODY>
    
    <center>
    <form name=GearCalc>
    Type Of Gear 
    <select name="shape" size="1" onChange="this.form.area.value = gearCalc1(this.selectedIndex); this.form.gear.value = gearCalc2(this.selectedIndex);">
    <option> ...
    <option value="square">45/80 gear
    <option value="triangle">80/125 gear
    <option value="rectangle">no teeth
    <option value="circle">0-25 gear
    <option value="sphere">conservative
    <option value="TG-22">TG-22
    </select>
    = <input type=text name=area size=10>
    <p>
    <input type=text name=gear size=10>
    </form>
    </center>
    
    <p><center>
    Now I edited it so it does display it differently, but it's not the way I want it. The way I have iot now is that when its done with the first prompt it gives you the answer and then it will display the prompts again...

    Anyway to fix this?

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    function gearCalc1(shape) {
      switch (shape.selectedIndex) {
        case 0 : return (""); break;
        case 1 : var pDiameter = prompt("Please enter the Pitch Diameter:", "");
    	var fDiameter = prompt("Please enter the Fit Diameter:", "");
    	var rbDiameter = prompt("Please enter the Rotor Bore Diameter:", "");
    	var rpm = prompt("Please enter the RPM:", "");
    	var gaWidth = prompt("Please enter the Gear Axial Width:", "");
    	var gfWidth = prompt("Please enter the gfWidth:", "");
             
            shape.form.area.value = pDiameter * fDiameter * rbDiameter * rpm * gaWidth * gfWidth; 
            shape.form.gear.value = Number(pDiameter) + Number(fDiameter) + Number(rbDiameter) + Number(rpm) + Number(gaWidth) + Number(gfWidth);
            break;
       //...other cases
    }
    ...
    <select name="shape" size="1" onChange="gearCalc(this)">
    If the math operation is addition, you need to convert the input string to number by using Number, parseFloat or parseInt methods because the + operator has 2 uses: addition and string concatenation.
    If operation is multiplication, division, or subtraction, you don't need the conversion as the strings are internally converted to numbers.

  • #4
    New Coder
    Join Date
    Dec 2004
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much


    As you can tell I'm new to java script. But anxious to learn


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •