I am missing something [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 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.

Related

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/

'Undefined' JSON data [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
This is my first time using JSON. I have an external json file and I am using the json file in my html file to create divs with every json data read. I can get the correct output for all of the values, except for one which is the 'descript' value. I have tried printing it to the cosole log but all I'm getting for that particular value is 'undefined' but the other values are alright. Any idea on why this happened?
JSON file:
[{"title":"3G","filePath":"https://example.com","descript":"hello world"}, {"title":"4G", "filePath":"https://example.com", "descript": "test"} ]
HTML file:
$.ajax({
url : "testJSON.json",
type : "get", // whichever you like
contentType:"json",
success : function(list)
{
var divCol = "<div class='col-sm-4 col-md-4'>";
var divWell = "<div class='well'>";
var divClose= "</div>";
list.forEach(function(obj, index) {
var title = "<h5>" + obj.title + "</h5>";
var desc = "<p>" + obj.descript + "</p>";
var linkStart = "<a class='btn btn-default' style='float:left' href='" + obj.filePath + "' target='_blank'>";
var linkEnd = "CSV</a>";
var div = divCol +
divWell +
title +
desc +
linkStart +
// image +
linkEnd +
divClose +
divClose;
console.log(list)
$("#imdaFiles").append(div); // insert the div you've just created
})
}
});
Just a short notice that might solve the issue.. you misplaced a double quote on the right side of the last descript key in your JSON.

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];

Why does this function return NaN? [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 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;
}

Categories

Resources