0

Here is my current javascript. I know that it worked until I added the 'for loop'. The point of the for loop is to switch letters. As it stands, its checking to see if groups of 3 are equal to one letter. I will fix that later, but I know there must be a syntax error, as the buttons don't fade as my mouse moves over them. I know this is a stupid question and I'll kick myself when I see the error, but I'm tired and can't find it for my life right now. Also, is this for loop a valid way of changing letters? Is that the correct way to set an array value to the new letter?

Here is the script.js:

    $(document).ready(function() {
    $('#button_translate').mouseenter(function() {
        $('#button_translate').fadeTo('fast', 1);
    });
    $('#button_translate').mouseleave(function() {
        $('#button_translate').fadeTo('fast', 0.7);
    });
    $('#button_clear').mouseenter(function() {
        $('#button_clear').fadeTo('fast', 1);
    });
    $('#button_clear').mouseleave(function() {
        $('#button_clear').fadeTo('fast', 0.7);
    });
    $('#button_translate').click(function() {
        var dna = $('input[name=dna]').val();
        var dna = dna.toUpperCase();
        function allBases(text) {  
            var bases = /^[ACGT]+$/;  
            if(text.match(bases)) {  
                var arr = text.match(/.{1,1}/g);
                /*document.write(arr + " is a DNA sequence.");*/
                for (var i = 0; i < (dna.length); i++) {
                    if (arr[i]==='A') {
                        arr[i]='U'
                    }else if (arr[i]==='C') {
                        arr[i]='G'
                    }else if (arr[i]==='G') {
                        arr[i]='C'
                    }else if (arr[i]==='T') {
                        arr[i]='U'
                    }else{
                        document.write(dna + " is not a real DNA sequence. Error Code 2");
                    }
                }
                document.write(dna + " is the translated mRNA strand!");
            }else{
                document.write(dna + " is not a real DNA sequence.");
            }  
        }

        allBases(dna);

    });
});
2
  • Should you be redeclaring the array elements. var arr[i]='U' becomes arr[i]='U'? Commented Aug 12, 2013 at 2:01
  • Updated the code. Still doesnt work Commented Aug 13, 2013 at 3:05

2 Answers 2

2
for (var i=0; i>=((dna.length) / 3); i++) {

you probably want the loop condition to be i less than dna.length

for (var i = 0; i < (dna.length/3); i++) {
Sign up to request clarification or add additional context in comments.

3 Comments

what does it do? I note you've dropped the division by 3. was this intentional?
I have intentionally dropped the division, and I'm using the "dna" string opposed to an array, and it still doesn't work, even if I only enter one letter in the text box.
yes, you won't be able to access parts of a string with arr[index]. That's array access syntax, for a string, you'd have to use String replacement MDN reference
0

you have an unclosed ( in the for loop

for (var i=0; i>=(dna.length) / 3); i++) { // extra ( in i>=((dna.length) / 3)

2 Comments

but I want its length divided by 3, is the extra pair of parentheses un-needed? Does it confuse the program?
@RileyLloyd the extra ( is not needed because the expression will be processed based on operator precedence where . operator has higher precedence than >= operator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.