error with javascript: missing ; before statement [closed] - javascript

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.

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");

JavaScript: Unexpected token [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
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();

comparing current date and date inp [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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 wanted to know whats the error in this code. i wanted to compare today's date with the user input date
<script type="text/javascript">
function validate()
{
if( document.myForm.name.value == "" )
{
alert( "Please provide your date of birth!" );
document.myForm.dob.focus() ;
return false;
}
var q = new Date();
var m = q.getMonth();
var d = q.getDate();
var Y = q.getFullYear();
var date = new Date(Y,m,d);
var r = document.getElementById(dates).value;
var m1 = r.getMonth();
var d1 = r.getDate();
var Y1 = r.getFullYear();
var mydate = new Date(Y1,m1,d1) ;
if(date<=mydate)
{
alert("greater");
return false;
}
else
{
alert("smaller");
return false;
}
return true;
}
</script>
<form name="myForm" method="post" onsubmit= "return(validate());">
Name: <input type="text" name="name"><br>
Date: <input type="date" id="dates" name="dates">
<br/>
<INPUT TYPE="submit" VALUE="Submit" >
</form>
i felt like "var r = document.getElementbyid(dates).value;" this piece of code is not working
The function name should be:
document.getElementById
(you are missing some capital letters)
Also, you are treating the result of document.getElementById(dates).value as though it will give you a Date object back, but it will actually give you are string - you need to convert this into a Date before you can call getMonth/getData/getFullYear on it - the answers to this question should help you here.
I have put comments below where the main errors in the code are.
function validate()
{
if( document.myForm.name.value == "" ) // Not an error per se, but you should use === to compare strings this way
{
alert( "Please provide your date of birth!" );
document.myForm.dob.focus() ;
return false;
}
var q = new Date();
var m = q.getMonth();
var d = q.getDate();
var y = q.getFullYear();
var date = new Date(Y,m,d); // The variable Y is not defined. You probably meant lowercase y.
var r = document.getElementbyid(dates).value; // Wrong case. Should be: document.getElementById
var m1 = r.getMonth();
var d1 = r.getDate();
var y1 = r.getFullYear();
var mydate = new Date(Y,m,d) ; // Same thing, probably meant lowercase y again.
if(date<=mydate)
{
alert("greater");
return false;
}
else
{
alert("smaller") // This statement is missing a semicolon
return false;
}
return true;
}
Also, it's not entirely clear what you are trying to do. At the end, assuming you fix those syntactical errors, you have three Date variables:
q, which is created with the current date and time.
date, which is just passed the same current date and time that was in q.
mydate, which... also holds the current date and time, since you just passed it the same values from q that you passed to date.
So since the two date objects you are comparing are set to the same date, you will always alert "greater" (which is not really greater, because they are equal, but that is a separate issue).
I also find it strange that this validation function returns false in all cases. Because of your if/else statement, there is no way to ever return true. For a function that is supposed to validate something, it seems odd to have the validation always return false.
Additionally, as #codebox stated in his answer, document.getElementById(dates).value is just returning a string, which you must then convert into a new Date object if you wish to read the date values from it into your m1, d1, and y1 variables (which, as of right now, are never even used).
You are having couple of typo and logic mistake. This code should work.
function validate()
{
if( document.myForm.name.value == "" )
{
alert( "Please provide your date of birth!" );
document.myForm.dob.focus() ;
return false;
}
var q = new Date();
var m = q.getMonth();
var d = q.getDate();
var y = q.getFullYear();
var date = new Date(y,m,d);
var r = Date.parse(document.getElementById("dates").value);
if(date<=r)
{
alert("greater");
return false;
}
else
{
alert("smaller");
return false;
}
return true;
}

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()+',
};

Type error in js [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I typed the following code
var curdte=new Date();
var curdteyr=curdte.getYear();
var curdtemh=curdte.getMonth();
var curdtedy=curdte.getDay();
var dtecurr=(curdtedy "-" curdtemh "-" curdteyr);
var fyear1=fromdat1.substring(6,10,10);
var fmonth1=fromdat1.substring(3,5,10);
var fday1=fromdat1.substring(0,2,10);
var fmdiff=(fday1 "-" fmonth1 "-" fyear1);
var frmdate1=(fyear1 "-" fmonth1 "-" fday1);
var tyear1=todat1.substring(6,10,10);
var tmonth1=todat1.substring(3,5,10);
var tday1=todat1.substring(0,2,10);
var todiff=(tday1 "-" tmonth1 "-" tyear1);
var todate3=(tyear1 "-" tmonth1 "-" tday1);
var oneday=24*60*60*1000;
var frmdiff=new Date(Math.ceil(dtecurr.getTime()-fmdiff.getTime())/oneday);
Then I get the error :
TypeError: dtecurr.getTime is not a function
That is because dtecurr is not a Date object, it should be a formatted string but is missing the + for concatenation. I've passed dtecurr to Date.parse, which should create a valid Date object to call getTime on:
var frmdiff=new Date(Math.ceil(Date.parse(dtecurr).getTime()-fmdiff.getTime())/oneday);
dtecurr is a string. Just because it is formatted like a user-friendly date, doesn't mean it's an instance of Date.
I think you want
var dtecurr=new Date(curdtedy + "-" + curdtemh + "-" + curdteyr);
instead of
var dtecurr=(curdtedy "-" curdtemh "-" curdteyr);
That is because dtecurr is not a Date object here.
Make it a Date object:
var dtecurr = new Date();
And then set values by methods setDate(), setMonth() and setFullYear()

Categories