unexpected token error in the code - javascript

thr below is my code, i try to make tdsum an array, but some how error shows up and i cannot fix it no matter what i did, can anyone please help me, i have tried to replace ntr with numbers and other characters, but it does not work either, so i suspect that there is something i missed that is not related to the ntr part
var tdsum = [];
function find(element,ntr) {
alert(element.cells[0].innerHTML);
var tr_sum = element.parentNode.childElementCount
var tdsum[ntr] = element.childElementCount;
alert(ntr);
alert(tdsum[2])
}
$('#row2').click();

You don't declare elements in an array. You only need to declare the array itself.
Remove the var on this line:
tdsum[ntr] = element.childElementCount;

Related

Why is javascript variable overlapping?

My question is why I'm having issues with declaring these variables. It works fine until "priceAdj" and the only thing I can find to explain that, Is that Javascript thinks it's still part of the previous variable. But I have no idea why it would think that because isn't the comma supposed to seperate them? I know I could just declare var again, But my code won't let me pass it up.
CODE:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
Logger.log(priceDigits);
}
I'm getting a syntax error of "Unexpected token )" when I run it in the console. You have an extraneous "/" in this line: (Math.ceil(priceAdj*100)/);
So I think I sort of know whats wrong.. the code actually does work, but I thought it didn't because what I'm using didn't make it the same color as the other variables for some reason. I don't know why that is, but I guess that isn't a question for here.A screenshot of the code is below:
Screenshot
Your var declaration is correct.
You can test it in codepen with the following sample which returns 288000 for priceDigits:
function myFunction() {
var average = (80),
totalDay = (100),
priceInt = (3000),
adjust = ((totalDay*priceInt)/average),
percent = ((priceInt/adjust)-1),
pLimit = 1+(percent)*(percent)*((percent)/Math.abs(percent)),
priceAdj = (priceInt*(pLimit)),
priceDigits = (Math.ceil(priceAdj*100));
//Logger.log(priceDigits);
return priceDigits;
}
$('body').text(myFunction());
See corresponding codepen here: https://codepen.io/anon/pen/YEPMLb.
Maybe your problem comes from your last instruction.

In Javascript I want to add a value to an array but in console i have the error "missing ; before statement"

I have this code:
for(var i=0;i<ordini.length;i++){
var quantitaordineinvio[i] = quantitaordineponte;
}
When I run it I get the error missing ; before statement.
But I just can not figure out what 's wrong . Can you help me?
var is for declaring variables. That's not what you're doing inside your loop; inside your loop, you're adding entries to an array. So remove the var from it.
If quantitaordineinvio hasn't been declared yet, you do want to declare it, just once, above the loop. You also want to initialize it, I'm guessing in this case with an empty array ([]):
var quantitaordineinvio = [];
for(var i=0;i<ordini.length;i++){
quantitaordineinvio[i] = quantitaordineponte;
}
Side note: Your adding the same value to the array repeatedly (just in case that's not what you meant to do).
Or just this line in ES6 with a map method :
var quantitaordineinvio = ordini.map(item => quantitaordineponte);

Javascript: Variable breaking function

Original content for context:
When ever I try to add var text0 = document.getELementById("text0"); to the following script, it breaks (it works without the text0 variable definition) how can I fix this?
Script:
function extend0 ()
{
var nav0 = document.getElementById("nav1");
var text0 = document.getELementById("text0");
{
nav0.style.paddingBottom = ("100px");
nav0.style.paddingLeft = ("100px");
nav0.style.paddingRight = ("100px");
text0.style.opacity = ("1");
}
}
Edit, after a few years:
Please be careful with your code and look out for syntaxical and spelling errors, this includes mis-casing, JavaScript is a case-sensitive language meaning it doesn't 'understand' if you use the wrong case. Ensure you look over your code for any errors, especially in new lines of code that you have just deployed, causing breakages.
Javascript method names are case-sensitive. You're calling getELementById, not getElementById (with a lower-case L).

Pass in vars to an eval workaround to process a code string from a json object after YUI compression

I need to pass a jQuery object in to a workaround for an eval. The issue is that i need access to a jQuery object that is out side the eval area but i can't see to pass it in. here is what i have.
var jObj = $(selector);
var myCode = "var jObj="+jObj+"; var i="+i+"; "+shape.mouseover.onEnd.replace("\u0027","'");
var myFucn = new Function(myCode);
myFucn();
the oject I'm getting the string out of is
shape.mouseover.onEnd.replace("\u0027","'");
is working and what I'm passing in that string is
open_info(jObj,i)
Which is what i have to fire. The deal is that the code is run thru YUI compressor so the jObj var becomes something else so i need to pass that in. Right now i get an error where it thinks it should have and ending ] which is not right. I is working it seems, just not the jObj var.
EDIT
there are many way to get where i need to be that are close but not quite like
How to pass parameters in eval in an object form?
shape.mouseover.onEnd = "open_info(jObj,i)";
/*
* this is coming in and must be as it is, don't say it's wrong please
* it's not able to be done anyother way!
*/
//lets process the string and pull in the vars
/* BEOFRE YUI COMPRESSOR CHANGES THINGS and works!!!
var jObj = $(selector);
var i = 1;
var myCode = shape.style.events.mouseover.onEnd.replace("\u0027","'");
var myFucn = new Function(myCode);
myFucn();
*/
// AFTER note it can be random as i change code so it fails cause
// var jObj is now var r and var i is now var e
var r = $(selector);
var e = 1;
var p= shape.style.events.mouseover.onEnd.replace("\u0027","'");
var f= new Function(p);
f();
Now it works before the compression.. After is not due to the change. Hope tha tclears it up some
I might be going down the wrong tracks and be confused here..
But isnt this what your trying to do?
Send myFucn the correct object and what ever i is
myFucn($(selector),10);
function myFucn(jObj,i)
{
shape.mouseover.onEnd.replace("\u0027","'");
}
I still don't understand why this question got 2 down votes, but well it's solved and works great. The trick is to do the same manipulation of the dom state. It's really simple once it is placed out.
//so this is what the object is parsed out to from the json string
//since you can't just pass a function stright that way any how
shape.mouseover.onEnd = "open_info(jObj,i)";
//this is what will take that string and process it
//note jObj is what is in the orgain code but it changes to
// var r or something else that is shorter after going thru YUI compressor
// Which is why we can't just use open_info(jObj,i) and it work..
// ie: it's not an issue with scoope but an issues with var names being shortened
(function(){
//this is the trick on passing them so YUI doesn't get them
//use a string and YUI skips it so we directly create the
//needed oject in the window namespace
window['jObj']=jObj; window['i']=i;
var p= shape.mouseover.onEnd;
var f= new Function(p);
f();
})();
That is it.. I put it in a click or hover event so it's kin to an onClick.
/* EXMAPLE OUTPUT AFTER YUI COMPRESSION
//Note after the YUI compressor get ahold of that
//processing code above it'll look like
*/
function(){window.jObj=n,window.i=t;var u=i.mouseover.onEnd,r=new Function(u);r()}();
So the way that works is, I needed to fix the issue of the var jObj being renamed. So I simply made a sting for the name and let the compressed the var name fill the name of the object I need for the processed code string. Don’t know why I didn’t see it before and I would have saved my rep value :-\ .. oh well. May be a way to shorten this but I'm leaving it for now.
Edit
I recant the edit it was working. :) Very well.. Left wondering what any other ways there would be to make it do the same thing.

Quotes in Javascript

I have been trying for hours to fix this code, I can't see what's wrong:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal('+s_d+','+ddmmyy+')">'
The problem is in href="javascript ..."
s_d is a javascript variable defined as
var num = 2;
var s_d = "sname"+num;
var ddmmyy = "ddmmyy";
Basically I need to call a javascript function with different parameter each time.
Use a backslash like \'.
document.getElementById('detail'+num).innerHTML=
'<a class="dobpicker" href="javascript:NewCal(\''+s_d+'\',\''+ddmmyy+'\')">'
Since this is the value of a href attribute, HTML encode them:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal("'+s_d+'","'+ddmmyy+'")">'
Or better yet don't use the javascript: protocol:
[0,1,2,3,4,5].forEach(function(num) {
var s_r = "sname"+num;
var ddmmyy = "ddmmyy";
var aEl = document.createElement("a");
aEl.className = "dobpicker";
aEl.onclick = function() {
NewCal(s_d, ddmmyy);
}
document.getElementById('detail'+num).appendChild(aEl);
});
Your .innerHTML setting is using s_d, but your variable declaration has s_r.
EDIT: That was the first thing that jumped out at me. Having looked a bit closer and realised the values are strings, I think fixing the variable name together with adding some escaped quotation marks as in Daniel A. White's answer will do the trick.

Categories

Resources