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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts

    EASY - if condition needs help

    Clicking the button does nothing, whereas it should alert me with a msgbox. Debugging my JS leads me to believe my if statement is incorrect. Am I using the wrong "and"?


    Javascript (FYI the js file is named getcalcs.js)

    Code:
    function calcs(){
    
    var nx = 0
    var T1x = 1
    var T2x = 1
    var T1 = 300
    var T2 = 465
    
    
    if var nx = 0 && T1x = 1 && T2x = 1 {
    var nf = 1 - (T1 / T2);
    }
    else{
    var nf = 0;
    }
    alert(nf)
    }

    HTML Code
    Code:
    <html>
    <head>
    <script type="text/javascript" src="getcalcs.js">
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Click me!" onclick="calcs()" />
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    Try updating your JS:

    Code:
    var nx = 0;
    var T1x = 1;
    var T2x = 1;
    var T1 = 300;
    var T2 = 465;
    var nf = "";
    
    if (nx == 0 && T1x == 1 && T2x == 1) {
    nf+= 1 - (T1 / T2);
    }
    else{
    nf+= 0;
    }
    alert(nf);
    }

  • Users who have thanked EpicWebDesign for this post:

    bagofmilk (05-27-2012)

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks! I had to change "nf+" to "nf", then it worked great.

    I appreciate this

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    You're welcome! Glad it worked out for you. I assume the actual variable data (T1x=1;, etc.) will probably be generated dynamically based on user input, so a few more tweaks may be necessary, but I'm confident you have the general idea of the script construct to deal with that. If not, please feel free to post your code again or PM me. Always happy to help!

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    The prodigal son has returned. You are correct, the values are actually user inputs. Here is what I have, and I'm not getting any alert messages, so something is wrong with my syntax. I'm wondering if any of the variables i'm using are not supposed to be used (example: I know you can't use "name" or "id" as a variable)


    The JS below is called "calc.js"
    Code:
    function ncalc() {
    
    var n = parseFloat(ntxt.value);
    var r = parseFloat(rtxt.value);
    var rp = parseFloat(rptxt.value);
    var mep = parseFloat(meptxt.value);
    var bwr = parseFloat(bwrtxt.value);
    var k = parseFloat(ktxt.value);
    var cp = parseFloat(cptxt.value);
    var cv = parseFloat(cvtxt.value);
    var Wnet = parseFloat(Wnettxt.value);
    var Win = parseFloat(Wintxt.value);
    var Wout = parseFloat(Wouttxt.value);
    var Qin = parseFloat(Qintxt.value);
    var Qout = parseFloat(Qouttxt.value);
    var T1 = parseFloat(T1txt.value);
    var T2 = parseFloat(T2txt.value);
    var T3 = parseFloat(T3txt.value);
    var T4 = parseFloat(T4txt.value);
    var p1 = parseFloat(p1txt.value);
    var p2 = parseFloat(p2txt.value);
    var p3 = parseFloat(p3txt.value);
    var p4 = parseFloat(p4txt.value);
    var v1 = parseFloat(v1txt.value);
    var v2 = parseFloat(v2txt.value);
    var v3 = parseFloat(v3txt.value);
    var v4 = parseFloat(v4txt.value);
    var u1 = parseFloat(u1txt.value);
    var u2 = parseFloat(u2txt.value);
    var u3 = parseFloat(u3txt.value);
    var u4 = parseFloat(u4txt.value);
    var nf = "";
    
        if (nx == 0 && T1x == 1 && T2x == 1){
        nf = (1 - (T1/T2))*100;
    	alert(nf);
    	}
    	else{
    	alert("nope");
    }
    }
    Here's snippets of my html:

    Code:
    </style>
    <script type="text/javascript" src="getdata.js"></script>
    <script type="text/javascript" src="calc.js"></script>
    </head>
    Code:
    <button type="submit" name="btnsub" id="btn1" class="btnsub" onclick="ncalc()">Submit</button>

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    Can you show the html code for the form fields? That would help a lot. I'm assuming your field id's are actually "ntext" "T1text" "T2text" etc. and they are all numeric.

    In the meantime, I can tell you the main problem I can see right away. It's here:
    Code:
       if (nx == 0 && T1x == 1 && T2x == 1){
        nf = (1 - (T1/T2))*100;
    	alert(nf);
    	}
    	else{
    	alert("nope");
    }
    }
    That snippet of code was written earlier when you had hard-coded values for each of those variables. Nowhere in your new code did you specify nx, T1x or T2x as variables, so it's looking for nothing at this point

    Try changing it to this:
    Code:
      if (n == 0 && T1 == 1 && T2 == 1){
        nf = (1 - (T1/T2))*100;
    	}
    	else{
        nf = "nope";	
    }
    alert (nf);
    }
    and then fill in those fields with 0, 1 and 1 respectively. That should return the calculation you want. Then test it again with different values and it should return the "nope" alert.
    Last edited by EpicWebDesign; 05-27-2012 at 09:44 PM. Reason: added suggested code changes

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Good point.

    Here's my crappy code (i just starting teaching myself html/js a few weeks ago, so bear with me). This forum would not let me upload .js and .html filetypes, so i renamed them to .txt.

    Just rename them:

    calc.js
    getdata.js
    otto.html
    Attached Files Attached Files

  • #8
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I want to have two different variables. One will be to validate if the textbox is being used, and the other is the value that they used.

    Example

    T1x -- This variable is used to validate if the "T1txt" Textbox is being used.
    T1 -- This variable is used to define the value the user put into "T1txt".

    You will see in the html (and getdata.js) that when the user clicks a checkbox, then an adjacent textbox will appear, else stay hidden. The user will likely only check maybe 15-20% of the total checkboxes, so I need to determine which values they want to define.

    I want to use the "..x" variable to validate that they are defining the value and the variable without the x to be the actual numeric value.

    I think it will make it much easier when I code my many formulas to have a 0 or 1 to signify the value than to write code to see if its checked or not.

  • #9
    Regular Coder
    Join Date
    Apr 2012
    Posts
    165
    Thanks
    1
    Thanked 39 Times in 39 Posts
    ok, I think I see where you are going with this, but I'll check out the files and see what i can do to help. I'm in the middle of making dinner so bear with me. I'm checking the laptop on the kitchen island in between cooking stages (while trying to keep my naughty dogs from grabbing food off the counter)

    EDIT: I've downloaded your files and am playing around with them to simplify your code. However, it is a holiday weekend here (Memorial Day) so we are having a bit of a party tonight and guests have started arriving early. I'll get back with you in the morning with some simplified code samples.
    Last edited by EpicWebDesign; 05-27-2012 at 11:47 PM.

  • #10
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks Guys. I totally understand about it being memorial day.

    I dumbed down the code, to only include the items im interested in.

    Here's my dumbed down HTML without my CSS. Also, you might notice I used the noob route of using a million &nbsp instead of properly aligning my checkboxes. If you know of any quick way to align them that would also be awesome. If its too much to explain then don't worry about it.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style></style>
    <script type="text/javascript" src="getdata.js"></script>
    <script type="text/javascript" src="calc.js"></script>
    </head>
    
    <body background="#ffffff">
    <form name="ottoform">
    <h1 id="title"> Otto Power Cycle (Air)</h1>
    
    <h1>General Properties</h1>
    
    <p>
    Thermal Efficiency (n)&nbsp;<input type="checkbox" name="nchk" id="nchk" onclick="a()"/>&nbsp;&nbsp;<input type="textbox" id="ntxt" size=5 style="visibility:hidden">&nbsp;%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
    </p>
     
     
    <h1>Stage 1</h1>
    <p>
    Temperature 1 (T1)&nbsp;<input type="checkbox" name="T1chk" id="T1chk" onclick="a()" />&nbsp;&nbsp;<input type="textbox" id="T1txt" size=5 style="visibility:hidden">&nbsp;
    <select size="1" name="dT1" id="dT1"style="visibility:hidden">
     <option selected value="C">C</option>
     <option value="F">F</option> 
     <option value="K">K</option> 
     <option value="R">R</option>&lt;\SELECT&gt;</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
    </p>
    
    <h1>Stage 2</h1>
    <p>
    Temperature 2 (T2)&nbsp;<input type="checkbox" name="T2chk" id="T2chk" onclick="a()" />&nbsp;&nbsp;<input type="textbox" id="T2txt" size=5 style="visibility:hidden">&nbsp;
    <select size="1" name="dT2" id="dT2"style="visibility:hidden">
     <option selected value="C">C</option>
     <option value="F">F</option> 
     <option value="K">K</option> 
     <option value="R">R</option>&lt;\SELECT&gt;</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
    </p>
    
    
    <button type="submit" name="btnsub" id="btn1" class="btnsub" onclick="ncalc()">Submit</button>
    </form>
    </body>
    
    </html>


    Here's my Javascripts (again, dumbed down for only the stuff I need).

    getdata.js
    Code:
    function a(){
    if(document["ottoform"]["nchk"].checked){
    document.getElementById("ntxt").style.visibility="visible";
    var nx = 1;
    }
    else{
    document.getElementById("ntxt").style.visibility="hidden";
    var nx = 0;
    }
    if(document["ottoform"]["T1chk"].checked){
    document.getElementById("T1txt").style.visibility="visible";
    document.getElementById("dT1").style.visibility="visible";
    var T1x = 1;
    }
    else{
    document.getElementById("T1txt").style.visibility="hidden";
    document.getElementById("dT1").style.visibility="hidden";
    var T1x = 0;
    }
    
    if(document["ottoform"]["T2chk"].checked){
    document.getElementById("T2txt").style.visibility="visible";
    document.getElementById("dT2").style.visibility="visible";
    var T2x = 1;
    }
    else{
    document.getElementById("T2txt").style.visibility="hidden";
    document.getElementById("dT2").style.visibility="hidden";
    var T2x = 0;
    }
    }

    Here's Calc.js
    Code:
    function ncalc() {
    
    var n = parseFloat(ntxt.value);
    var T1 = parseFloat(T1txt.value);
    var T2 = parseFloat(T2txt.value);
    var nf = "";
    
    if (nx == 0 && T1x == 1 && T2x == 1){
        nf = (1 - (T1/T2))*100;
        alert(nf);
    }
    else{
        alert("nope");
    }
    }

  • #11
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it's a scope problem. The variables that you are declaring in the a() function are local to that function, so not available to the ncalc() function. You should also avoid referring to elements by their bare id (which will only work in IE) - use getElementById() instead. I think your ncalc function will work better something like this:
    Code:
    function ncalc() {
    var n = parseFloat(document.getElementById("ntxt").value);
    var T1 = parseFloat(document.getElementById("T1txt").value);
    var T2 = parseFloat(document.getElementById("T2txt").value);
    var nf = "";
    
    if (document["ottoform"]["nchk"].checked && document["ottoform"]["T1chk"].checked && document["ottoform"]["T2chk"].checked){
        nf = (1 - (T1/T2))*100;
        alert(nf);
    }
    else{
        alert("nope");
    	}
    }

  • Users who have thanked xelawho for this post:

    bagofmilk (05-29-2012)

  • #12
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi BagofMilk,

    For this it is handy to debug the JavaScript without the browser in the way.



    The issue gets highlighted in the editor.

    Then fix the syntax and run the code again.



    The product http://www.scriptscraper.com I created to do this was initially designed to scrape html websites. But its also handy for generally fiddling with JavaScript.

    thanks,
    Martin.

  • Users who have thanked martsbradley for this post:

    bagofmilk (05-29-2012)

  • #13
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by martsbradley View Post

    The product http://www.scriptscraper.com I created to do this was initially designed to scrape html websites. But its also handy for generally fiddling with JavaScript.
    Hi Martin. Thanks for the link. It seems that this is mainly for compiling Javascript into Excel, as well as detecting javascript errors? Can this work the other way around, take Excel VBA code and convert to Javascript?

    The reason I ask is because the js im trying to accomplish (above), i have completely coded and working in Excel VBA, i've just been trying to re-create my program using javascript (so I can create a webpage from it).

    If ScriptScraper can't do this, do you know of any other program that might help convert Excel VBA to Javascript? (or any other programming language that html can read?)

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    *IF* your code is only supposed to work in MSIE browsers, the easiest thing to do is convert the VBA to VBScript, not JavaScript. Then the changes needs are almost miniscule. (VBScript doesn't have typed variables, so you can't do Dim cell As Excel.Cell, for example..but you can just use [icode]Dim cell[/b] and then just not worry about the types and most of the time it will work.)

    Of course, if you are trying to get this to work cross-browser, then you have not much choice but to just start rewriting from the ground up.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bagofmilk (05-30-2012)

  • #15
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks Old Pedant!

    I completely forgot about VBScript, but I do want this to work across the major browsers.

    Does anyone know of any good reference books/websites for JS or JQuery? Specifically ones that cater to beginners and offer examples? I've been learning a lot from this forum and w3schools, but would like a more comprehensive overview if possible.

    Thanks!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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