0

I am trying to loop through an object literal and map the substring to a value and plug it into an equation. Right now my code doesn't return an integer but instead is undefined. Here is the code:

function calcFunc(sequence) {
    var dH = 0;
    var dS = 108;
    var i;
    // Compute dH and dS
    for ( i = 0 ; i < ( sequence.length - 1 ) ; i++ ) {
        var pair = sequence.substr(i, 2);
        dH += nn_h[pair];
        dS += nn_s[pair];
    }
    dH *= -100.0;
    dS *= -0.1;
    return dH / ( dS + 1.987 * Math.log( 100 / 4000000000.0 ) ) - 273.15 +
    16.6 * ( Math.log( 50 / 1000.0 ) / Math.log(10) );   
}
12
  • Could you add a fiddle example ? Commented Nov 4, 2015 at 9:48
  • what is sequence in your calcFunc ? Commented Nov 4, 2015 at 9:50
  • example function call would be as follows: var sequence = "tctgtt"; calcFunc(sequence) Commented Nov 4, 2015 at 9:51
  • 1
    / log(10) should be / Math.log(10)? Commented Nov 4, 2015 at 9:51
  • Without knowing what your sequence looks like, could it be that you should increase i by 2 in your loop? Commented Nov 4, 2015 at 9:52

2 Answers 2

1

Looks like there are at least two errors:

  1. Trying to call log(10) instead of Math.log(10). This is likely causing the undefined error.
  2. Not converting the strings to integers when you load them from the nn_s and nn_h objects. This means you will concatenate the strings rather than add them together as integers.

    function calcFunc(sequence) {
        var dH = 0;
        var dS = 108;
        var i;
    
        // Compute dH and dS
        for ( i = 0 ; i < ( sequence.length - 1 ) ; i++ ) {
            var pair = sequence.substr(i, 2);
            dH += parseInt(nn_h[pair], 10); // parseInt
            dS += parseInt(nn_s[pair], 10); // parseInt
        }
    
        dH *= -100.0;
        dS *= -0.1;
    
        return dH / ( dS + 1.987 * Math.log( 100 / 4000000000.0 ) ) - 273.15 +
            16.6 * ( Math.log( 50 / 1000.0 ) / Math.log(10) );   // Math.log(10)
    }
    

Working Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Im fairly sure the third problem is not increasing the i by 2. Otherwise for the input specified (tctgtt) there is a value for tc but not one for ct
@Jamiec Possibly, that depends on the possible sequence values.
0

As I stated:

  • Math.log() is missing
  • Values are not numbers
  • Increment should be 2
  • Some keys and values are missing (here filled with 0)

The missing values can be replaced by a default value like

dH += nn_h[pair] || 0;
dS += nn_s[pair] || 0;

var nn_s = {
    tc: 135,
    tg: 129,
    tt: 240,
    tn: 168,
    na: 168,
    nc: 210,
    ng: 220,
    nt: 215,
    nn: 203
};
var nn_h = {
    tc: 0,
    tg: 0,
    tt: 0,
    tn: 66,
    na: 66,
    nc: 85,
    ng: 91,
    nt: 80,
    nn: 80
};
function calcFunc(sequence) {
    var dH = 0;
    var dS = 108;
    var i;
    // Compute dH and dS
    for (i = 0 ; i < sequence.length ; i+=2) {
        var pair = sequence.substr(i, 2);
        dH += nn_h[pair];
        dS += nn_s[pair];
    }
    dH *= -100.0;
    dS *= -0.1;
    return dH / (dS + 1.987 * Math.log(100 / 4000000000.0)) - 273.15 +
    16.6 * (Math.log(50 / 1000.0) / Math.log(10));
}

document.write(calcFunc('tctgtt'));

Comments

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.