JavaScript: Unexpected token [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
Introduce the problem before you post any code:
I have a simple JavaScript code and it's giving me an unexpected token error. It's code that I'm trying to convert from PHP to JavaScript for use in a Phonegap application.
Specifically it is the line that declares the $ex_data variable. And it says the unexpected token is the section between the $system and $galaxy variables.
Code
for ($galaxy = 1; $galaxy < 21; $galaxy++){
for ($system = 1; $system < 601; $system++){
var $ex_data = '{"planet_id":-1,"sid":'.$system.',"language":"en","gid":'.$galaxy.'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign=".toUpperCase($sign);
}
}
CLEAR QUESTION:
How do I fix the line so that it is valid?

You are confusing PHP concatenation with JavaScript (C) concatenation.
for ($galaxy = 1; $galaxy < 21; $galaxy++) {
for ($system = 1; $system < 601; $system++) {
var $ex_data = '{"planet_id":-1,"sid":'
. $system . ',"language":"en","gid":' . $galaxy.'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign="
.toUpperCase($sign);
}
}
Change the ".s" to "+s".
var $ex_data = '{"planet_id":-1,"sid":'
+ $system + ',"language":"en","gid":' + $galaxy + '}';
or alternatively
var $ex_data = JSON.stringify({
"planet_id" : -1,
"sid" : $system,
"language" :"en",
"gid" : $galaxy
});
Also, as pointed out below, change .toUpperCase($sign); -> + $sign.toUpperCase();

var $ex_data = '{"planet_id":-1,"sid":' + $system + ',"language":"en","gid":' + $galaxy +'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign=" + $sign.toUpperCase();

Related

"Missing ) after..." on a line, 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 2 years ago.
Improve this question
So I am creating a "Silly Story Generator" in Javascript and after fixing a few errors that popped up I encountered "SyntaxError: missing ) after argument list"
After reading more about it I learned that it occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.
I checked my code and I cannot seem to find the mistake, string on line 38 looks okay.
Thank you.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
Your Code is Badly formatted.
At newStory.contentString.replace("insertX", "insertY", "insertZ";)
You had a semi-colon inside the the parenthesis.
You are also missing two curly braces near the end.
I suggest getting a good IDE or using the formatting features that come with the one you use.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
you have written a semicolon before the closing parentheses
newStory.contentString.replace("insertX", "insertY", "insertZ");

error with javascript: missing ; before statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm getting an error with my javascript: "missing ; before statement".
I'm trying to read in a date, add 6 months onto the date if it meets a certain criteria ( joiner type in this case) and if not just return that date.
I can't see whats wrong here, it must be something small, any ideas??
Thanks!
function checkenddate(Par) {
var array = Par.split("!!");
var usermskey = array[0];
var date = array[1];
var joinertype = array[2];
saprep = UserFunc.uGetConstant("glb.REPOSITORY_ECC");
attr1 = "Z_VALIDTO" + saprep;
uWarning("Attribute: " + attr1);
if (date == null && joinertype.equals("Contractor"))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calender c = Calender.getInstance();
c.setTime(sdf.parse(date));
c.add(Calender.MONTH, 6);
enddate = sdf.format(c.getTime());
uWarning("End Date:" + enddate);
OutString = uIS_SetValue(usermskey, 0, attr1, enddate);
return enddate;
} else {
OutString = uIS_SetValue(usermskey, 0, attr1, date);
return date;
}
}
Thats not valid javascript. You can't have typed variables such as SimpleDateFormat sdf = new blah(). Change your types to var and it will work as expected.
var sdf = new SimpleDateFormat("yyyy-MM-dd");
var c = Calender.getInstance();
This is not how you declare a JS variable:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
You need this instead:
var sdf = new SimpleDateFormat("yyyy-MM-dd");
I would recommend using one of the linting tools online (e.g. JSHint or JSLint) to help track down these issues - very handy.

Javascript syntax error [Node JS] [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
I am learning nodeJS and I have this syntax error which I don't understand.
Can someone point out what is the syntax error, why I am getting it, and how do I bypass it?
var http = require('http');
var url = require('url');
var server = http.createServer(function(req,res) {
if (req.method == 'POST') {
return res.end("Only get requests");
}
var st = url.parse(req.url,true);
if (st.indexOf("parsetime") > -1) {
var time = st.substring(st.indexOf("iso"));
var date = new Date(time);
var out = '{
"hour":'+date.getHours()+',
"minute":'+date.getMinutes()+',
"second":'+date.getSeconds()+',
}';
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(out);
} else if (st.indexOf("unixtime") > -1) {
var time = st.substring(st.indexOf("iso"));
var date = new Date(time);
var out = "{
'unixtime':"+date.getTime()+"
}";
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(out);
} else {
return res.end("404");
}
});
server.listen(process.argv[2]);
The syntax error is on line 11 : " var out = '{ "
Remove the single quotes from here:
var out = '{
"hour":'+date.getHours()+',
"minute":'+date.getMinutes()+',
"second":'+date.getSeconds()+',
}';
Change the above to:
var out = {
"hour": date.getHours(),
"minute": date.getMinutes(),
"second": date.getSeconds(),
};
Or if I may be mistaken for the string to contain a JSON object, you need to do declare the out that way and stringify using:
out = JSON.stringify(out);
The problem is that you tried to have a multi-line string, which you can't do like that in JavaScript. It is probably easier to do it like this:
var out = '{';
out+='"hour":'+date.getHours(),
out+='"minute":'+date.getMinutes(),
out+='"second":'+date.getSeconds()
out+='}';
Or, even easier, just define the object, then use JSON.stringify() to turn it into a string:
var outObj = {
hour:date.getHours(),
minute:date.getMinutes(),
second:date.getSeconds()
};
var obj=JSON.stringify(outObj);
This just defines a normal object, then turns it into JSON
Remove quotes
var out = {"hour":'+date.getHours()+',
"minute":'+date.getMinutes()+',
"second":'+date.getSeconds()+',
};

Removing quotation marks from JSON encoded string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have a JSON encoded string generated by inputting values from a array, it is as follows -
"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"
I need to convert it to the following format :
"[{value: 97049},{value: 84866},{value: 39402},{value: 30250},{value: 33363}]"
Thanks.
$input = $json_var;
$input = str_replace( '"', '', $input ); // strip em
$input = '"' . $input . '"'; // wrap back around
JS:
var json_array = JSON.parse(json_string);
for (var i = 0; i < json_array.length; i++) {
json_array[i] = JSON.parse(json_array[i];
}
PHP:
$json_array = json_decode($json_string);
$json_array = array_map('json_decode', $json_array);
It would probably be better to fix this at the source. If it's supposed to be an array of objects, don't quote each array element before adding them to the array.
var myQuotedJson = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
var myUnquotedJson = myQuotedJson.replace(/"/, '');
You can do that like this:
var input = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
output = '"' + input.replace('"','') + '"';
//Alerts your output
alert(output );

Get values of Input[type=(type)] from specific class with jQuery [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
I have this code that does not work:
kw _class = _keyword1;
var text = $("'input." + kw_class + "[type=text]'").val();
var val = $("'input." + kw_class + "[type=hidden]'").val();
Firefox console comes out with this:
`Syntax error, unrecognized expression: 'input._keyword1[type=text]'
I have tried at least three combos of this that are not working that I found from other questions.
Yes because you have ' ' inside of the selector. It should be:
var text = $("input." + kw_class + "[type=text]").val();
var val = $("input." + kw_class + "[type=hidden]").val();
you have extra '' in the selector
var text = $('input.' + kw_class + '[type=text]').val();
var val = $('input.' + kw_class + '[type=hidden]').val();

Categories