www.webdeveloper.com
Results 1 to 5 of 5

Thread: NEW to JavaScript-->converting Python to JavaScript

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    Unhappy NEW to JavaScript-->converting Python to JavaScript

    Well... I am taking Python this semester. Our teacher wants us to convert Python to JavaScript without really explaining how or/and expecting us to be familiar with it already
    I googled lots of thing out but now I am stuck because I don't know what I did wrong, so I can formulate my question correctly;

    Python Program I have is pretty simple - it prompts you to enter your first and last name and end your DNA string, "CAG" repeats are counted by another function and the result is passed to function that identified weather you may have huntington's decease or not.

    Code below is my weak attempt to do the conversion:
    <body>
    <p>Enter First Name: <input type="text" size="25" id="firstname" value="">
    <p>Enter Last Name: <input type="text" size="25" id="lastname" value="">
    <p>Enter DNA: <input type="text" size="25" id="DNA" value="" >

    <p><button onclick="alertt(f,l,d,c,p)" style="background-colorink"><b>PRESS TO FIND OUT RESULT</b></button></p>

    <script>

    var f = document.getElementById('firstname').value;
    console.log(f);
    var l = document.getElementById('lastname').value;
    console.log(l);
    var d = document.getElementById('DNA').value;// DNA string
    console.log(d);
    var c = countCAG(d); // number of CAG repeats
    console.log(c);
    var p = prediction(c); //prediction
    console.log(p);

    function alertt(f,l,d,c,p)
    {
    alert ("First Name: " +f+ "Last Name: "+l+"DNA string"+d+"NUmber of repeats"+c+"Prediction"+p)
    }


    function countCAG(dna)
    {
    var index = 0;
    var index_2 = 3;
    var count = 0;
    while (index < dna.length)
    {
    if (dna.slice(index,index_2) == "CAG")
    {
    count+=1;
    }
    else
    {
    //break;
    index+=3;
    index_2+=3;
    }
    return count;
    }
    }

    function prediction(numCAG)
    {
    if (numCAG < 28)
    {
    return "Normal, Unaffected";
    }
    else if (36 >= numCAG >= 28)
    {
    return "Intermediate, Unaffected";
    }
    else if (42 >= numCAG >= 37)
    {
    return "Reduced Penetrance, Affected";
    }
    else if (numCAG > 42)
    {
    return "Full Penetrance, Affected";
    }
    else
    {
    return "invalid Count";
    }
    }
    </script>

    </body>
    When I run it there is an issue with countCAG and prediction functions I would run each one individually in JS console and I get Undefined, or InvalidString I read that I am supposed to use var in front of each variable, unlike in Python where you just assign variable a value without any add-ons at the front. But JS console seems to recognize both ways of assigning, however var smth=0 immediately evaluates to Undefined, and smth=0, gives me 0... As I said, I am so new to this, and all the basics I learned, I got from online pls don't make fun of me!
    SOMEONE PLS HELP, just point out what am I missing

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,389

    Lightbulb

    You have a number of logic and syntax errors in your original code.
    I took the liberty of changing it to eliminate the error warnings,
    but I don't know if I have somehow altered your logic reports.

    That part you will need to verify with some actual data.
    Remove the test entry values when you want to go for real.

    Compare your code to the modified code to see where changes have been made.

    Good Luck!


    BTW: You should enclose your script between [ code] and [ /code] tags (without the spaces)
    to retain the formatting and make it easier for forum members to read, copy, paste, debut, etc.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> DNA Sequence </title>
    </head>
    <body>
    <p>Enter First Name: <input type="text" size="25" id="firstname" value="">
    <p>Enter Last Name: <input type="text" size="25" id="lastname" value="">
    <p>Enter DNA: <input type="text" size="100" id="DNA" value="CAGAAGGAGCAGAAGGAGCAG" >
    
    <p><button onclick="checkCode()" style="background-color:pink">
       <b>PRESS TO FIND OUT RESULT</b>
    </button></p>
    
    <script>
    // From: http://www.webdeveloper.com/forum/showthread.php?291919-NEW-to-JavaScript-gt-converting-Python-to-JavaScript
    
    
    function checkCode() {
      var f = document.getElementById('firstname').value;  //console.log(f);
      var l = document.getElementById('lastname').value;   //console.log(l);
      var d = document.getElementById('DNA').value;        //console.log(d);  // DNA string
      var c = countCAG(d);                                 //console.log(c);  // number of CAG repeats
      var p = prediction(c);                               //console.log(p);  // prediction
      alert ("First Name: " +f+ "\nLast Name: "+l+"\nDNA string: "+d+"\nNumber of repeats: "+c+"\nPrediction: "+p);
    }
    
    
    function countCAG(code) {
      var dna = code.split('');
      var index = 0;
      var index_2 = 3;
      var count = 0;
      var cseq = '';
      while (index < dna.length) {
        cseq = dna.slice(index,index_2);
        if (cseq.join('') == "CAG") { count+=1; } // alert(count+' '+cseq); }
        index+=3;
        index_2+=3;
      }
      return count;
    }
    
    function prediction(numCAG) {
      if (numCAG < 28) { return "Normal, Unaffected"; }
      else 
       if (numCAG <= 36) { return "Intermediate, Unaffected"; }
       else 
        if (numCAG <= 42) { return "Reduced Penetrance, Affected"; }
        else
         if (numCAG > 42) { return "Full Penetrance, Affected"; }
         else { return "invalid Count"; }
    }
    
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Mar 2014
    Posts
    3
    Thank you so much! You did change the logic behind the assignment a bit, but I know how to fix it, but I now know where I screwed up
    Really appreciate it!

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,389

    Lightbulb

    Quote Originally Posted by mariabutenka View Post
    Thank you so much! You did change the logic behind the assignment a bit, but I know how to fix it, but I now know where I screwed up
    Really appreciate it!
    You're most welcome. Happy to help.

    Whenever you have multiple if..then..else statements and you don't put in the else { } brackets, it gets difficult to determine where the ranges are.
    My take on the problem is that whenever the if.then.else comparisons is greater than ONE, I go to a switch statement.
    Based on the logic I used as I understood it from the original post, this is how I would have managed the logic sequence.
    See it is not a bit easier to understand and modify (if needed).

    Code:
    function prediction(numCAG) {
      var n = Number(numCAG);   // forces string passed into a number
      var msg = '';
      var dx = -1;              // set for invalid entry
      if (n >  0 ) { dx = 0; }  // set if range = 1...27 
      if (n >= 28) { dx = 1; }  // set if range = 28...35
      if (n >= 36) { dx = 2; }  // set if range = 36...42
      if (n >  42) { dx = 3; }  // set if range > 42
      switch (dx) { 
        case 3 : msg = "Full Penetrance, Affected"; break;
        case 2 : msg = "Reduced Penetrance, Affected"; break;
        case 1 : msg = "Intermediate, Unaffected"; break;
        case 0 : msg = "Normal, Unaffected"; break;
        default: msg = 'Invalid Count'; break;
      }
      return msg;
    }
    Last edited by JMRKER; 03-17-2014 at 02:55 PM.

  5. #5
    Join Date
    Mar 2014
    Posts
    3
    That's a good one! Definitely cleans up the code, but our teacher wanted us to modify exactly the code we have in our original python program... as part of learning experience I guess
    but good to know...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles