Reputation: 221
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) );
}
Upvotes: 0
Views: 55
Reputation: 386604
As I stated:
Math.log()
is missing0
)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'));
Upvotes: 0
Reputation: 35793
Looks like there are at least two errors:
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)
}
Upvotes: 1