Why does this function return NaN? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
function getNucleobaseCount (strand) {
/*
Returns occurences of nucleobases A and C respectively
*/
var countA = (strand.split("A").lenght - 1);
var countC = (strand.split("C").lenght - 1);
return countA + " " + countC;
}
But
> console.log(getNucleobaseCount("AAGCATT"))
Nan Nan
Instead of the expected 3 1
Why?

The value of the lenght property will be undefined.
undefined - 1 is NaN
You misspelt length.

Misspelling of length? I would think that it would show a compilation error though.

you have
misspelled 'length' :-)
function getNucleobaseCount (strand) {
/*
Returns occurences of nucleobases A and C respectively
*/
var countA = (strand.split("A").length - 1);
var countC = (strand.split("C").length - 1);
return countA + " " + countC;
}

Related

getting NaN when calculating parsed integer difference [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a program that counts the number of lines in a text file and it works fine. What I am trying to do is count the number of lines in 2 different files and calculate their difference but I'm getting NaN I parsed them to integers why is it not a number? How can I calculate their difference? Thanks in advance.
filePath = process.argv[2];
fileBuffer = fs.readFileSync('filePath');
to_string = fileBuffer.toString();
split_lines = to_string.split("\n");
filePath2 = process.argv[2];
fileBuffer2 = fs.readFileSync('filePath2');
to_string2 = fileBuffer2.toString();
split_lines2 = to_string2.split("\n");
//logging NaN
console.log("Calc :" + parseInt(split_lines2.length) - parseInt(split_lines.length))
Lets take a close look at this line
console.log("Calc :" + parseInt(split_lines2.length) - parseInt(split_lines.length))
Since I don't have those var's, lets replace them with some demo numbers:
console.log("Calc :" + 10 - 5);
This will still return NaN because "Calc :10" - 5 fails.
If you enclose the sum in some brackets, there evaluated before adding to the string so it becomes "Calc :" + 5. Since JS will convert the 5 to a string, it producing the expected output:
console.log("Calc :" + (10 - 5));
So you're console.log should look something like:
console.log("Calc :" + (parseInt(split_lines2.length) - parseInt(split_lines.length)))
Because you do math together with "Calc :":
"Calc :" plus split_lines2.length, minus split_lines.length - no concatenation here.
Wrap calculations inside (), so your + becomes concatenation instead of addition
/* filePath = process.argv[2];
fileBuffer = fs.readFileSync('filePath');
to_string = fileBuffer.toString();
split_lines = to_string.split("\n");
filePath2 = process.argv[2];
fileBuffer2 = fs.readFileSync('filePath2');
to_string2 = fileBuffer2.toString();
split_lines2 = to_string2.split("\n"); */
var split_lines2 = [1, 2];
var split_lines = [1]
//logging NaN
console.log("Calc :" + (parseInt(split_lines2.length) - parseInt(split_lines.length)))

Need help to understand Vue.js function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an assignment to make calculator using vue.js.
Much of it works, I'm confused about how to implement the '=' function.
I found this piece of code in an article that does something similar but I don't understand what it does. Can someone explain it?
https://scotch.io/tutorials/build-a-scientific-calculator-with-vuejs
I found this Piece of code:
if ((app.current).indexOf("^") > -1) {
var base = (app.current).slice(0, (app.current).indexOf("^"));
var exponent = (app.current).slice((app.current).indexOf("^") + 1);
app.current = eval("Math.pow(" + base + "," + exponent + ")");
} else {
app.current =app.current
}
Can someone please explain what the above function does, line-by-line?
// checks if app.current have '^' by getting the index
// if the method indexOf doesn't find anything it return -1
if (app.current.indexOf('^') > -1) {
// gets the first element till the index where it found '^'
var base = app.current.slice(0, app.current.indexOf('^'));
// gets the number after the 'ˆ'
var exponent = app.current.slice(app.current.indexOf('^') + 1);
// eval is evil
// it gets the string and transfoms into valid code
// wich means to do the operation
app.current = eval('Math.pow(' + base + ',' + exponent + ')');
} else {
// if it doesn't find the index it keeps the same value
app.current = app.current;
}
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

I am missing something [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've looked this code over and over.. I typed it as I was learning from lesson material, and have checked and checked over and over again. the code works for the instructor, but not me. Is it my mistake or is it a problem for the platform I am using? It not working may seems trivial comparing to needing to understand what is being taught, but I still would like to understand all that I can. thank you. https://codepen.io/Slimmwillis/pen/EQWpGP?editors=1111
function yearsUntilRetirement(name, year) {
var age = calculateAge(year);
var retirement = 65 - age;
if (retirement >= 0) {
console.log(name + ' retires in ' + retirement + ' years.');
} else {
console.log(name + ' is already retired.');
}
}
yearsUntilRetirment('John', 1990);
you need to be very careful while typing. You're not calling the right function so nothing happens.
function yearsUntilRetirement(name, year) {
var age = calculateAge(year);
console.log('hello');
var retirement = 65 - age;
if (retirement >= 0) {
console.log(name + " retires in " + retirement + " years.");
} else {
console.log(name + " is already retired.");
}
}
yearsUntilRetirment("John", 1990);
Check the yearsUntilRetirment("John", 1990); ! the name doesn't match the function.

Missing ; before statement in Google Script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
It says I have a Missing ; before statement on the "var api =..." line.
I'm not sure what else it is I need to do.
If you need to see the rest of the script, please let me know. I will gladly submit everything else.
function askWolframAlpha_(q, app) {
try {
var api = "http://api.wolframalpha.com/v2/query?podindex=2&format=plaintext&appid=" + 67ULAK-L9R928PL76 + "&input=" + encodeURIComponent(q);
var response = UrlFetchApp.fetch(api, {
muteHttpException: true
});
// Parse the XML response
if (response.getResponseCode() == 200) {
var document = XmlService.parse(response.getContentText());
var root = document.getRootElement();
if (root.getAttribute("success").getValue() === "true") {
return root.getChild("pod").getChild("subpod").getChild("plaintext").getText();
}
}
} catch (f) {}
return false;
}
The problem is the 67ULAK-L9R928PL76 in the following line:
var api = "http://api.wolframalpha.com/v2/query?podindex=2&format=plaintext&appid=" + 67ULAK-L9R928PL76 + "&input=" + encodeURIComponent(q);
Change to:
var api = "http://api.wolframalpha.com/v2/query?podindex=2&format=plaintext&appid=67ULAK-L9R928PL76&input=" + encodeURIComponent(q);
or at least to:
var api = "http://api.wolframalpha.com/v2/query?podindex=2&format=plaintext&appid=" + "67ULAK-L9R928PL76" + "&input=" + encodeURIComponent(q);

elements is undefined in Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Hello I am getting error "elements is undefined" in Javascript
function ImportExcelMapping()
{
debugger;
var str = "";
for (var i = 0; i < document.forms[0].elements.length; i++ ) {
if (document.forms[0].elements[i].type == "hidden") {
str += '<input type=\"hidden\" name="' + elements[i].name + '" value=\"' + elements[i].value + '">';
}
}
// rest of function
}
please help
elements[i].name
Where is elements defined? Is that supposed to be document.forms[0].elements[i]?
EDIT:
Since you seem to be confused still, I'll add a bit more detail. Hopefully this clears it up.
You either need to declare elements at the top of your function, like so:
function ImportExcelMapping() {
debugger;
var str = "";
var elements = document.forms[0].elements;
// rest of function
}
Or
You need to change the line that's breaking to this:
str += '<input type=\"hidden\" name="' + document.forms[0].element[i].name + '" value=\"' + document.forms[0].element[i].value + '">';
Well you never defined elements. You probably just need to declare it:
var elements = document.forms[0].elements[i];

Categories

Resources