Changing a string into an integer - javascript

This is a follow up question to this:
Google Analytics Event Tracking via a jQuery plugin
Matt Austin was correct. I was passing a string of the integer for the GA value parm when I should have been passing the int value itself. Evidently Google Analytics is sensitive like that :)
So I changed: parmValidatedObject[key] = val;
to: parmValidatedObject[key] = val.valueOf();
But that doesn't seem to be working as expected. GA doesn't seem to pick this up as an int. What am I not understanding about valueOf()?
btw, I might also run into something similar with one of the other parms that's a boolean. I'm translating those strings to the value or 0 or 1 but I'm wondering (out loud and in advance) if that's the right approach.

parseInt(val,10); will convert your string to an integer. It is a JavaScript function, not jQuery.
!!val converts a value to a boolean. Note that "0" is considered a truthy value and so !!"0" returns true, unlike the false you might expect. In this case, you may be better off using
(""+val == "0") ? false : true;

Related

Google Earth Engine ee.Number to integer

This is probably a simple question for people familiarized with the Code Editor of Google Earth Engine (https://code.earthengine.google.com/) or generally Javascript.
In my code, I need to use the size of an object for a boolean conditional (e.g. n>0). However, the output of .size() which I would store in n does not return a plain integer, but a ee.Number structure and I am not being able to transform it into an integer to properly evaluate the conditional.
Example with the structure ee.Number of Earth Engine:
var n=ee.Number(1)
print(n)
print(n.int())
print(n==1)
print(n===1)
print(n.int()==1)
print(n.int()===1)
print(n.int()== parseInt(1))
This outputs these evaluate as false, even when I try to tast the number structure into an int.
1
1
false
false
false
false
false
note:
print(typeof n)
returns an object (JSON):
object
Any help very much appreciated. Thanks
This is due to how GEE works. Processing steps are constructed locally as objects and then only evaluated by the server once another function requires it.
print is one of the functions that requires execution, this is why it shows as integer in your console.
You can force evaluation with .getInfo()... this however should be used with caution, because everything is pulled to the client side, which can be problematic with big objects.
So this works:
var n=ee.Number(1)
print(n)
print(n.getInfo()==1)
giving
1
true
This section of the documentation explains the background.
If the value of n indeed is JSON, try to parse it:
n = JSON.parse(n);
Then convert it into an integer:
n = parseInt(n);

javascript losing decimals during assignment

I am trying to do a simple thing but for some reason it is not working. I am using Knockout and I have a model which I update after user enter some data and use the same to communicate back to C# code on server side. For some reason, when I try to assign decimal value to one of the member of model it isn't working. Though, in this case I am using knockout, I believe it has nothing to do with KO. See the screenshot where I have the value 22.78 and I am trying to do parseFloat but it ends up as just 22. I tried other things such as removing he parseFloat just to see if it accepts the string value as it is but even that is not working. Can someone help?
Your doing Bitwise OR while assigning the value.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) | 0;
so only it returns 22. Because 22.78 | 0 is 22.
Please check this code.
console.log(22.78 | 0);
Please try this code while assigning value. You can get decimal values without loss.
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED);
Pelase Check below link for more details.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR
try this (please notice the double || )
this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) || 0;

Get the value of confused Javascript code

I got a piece of code like this:
var password = eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('9 5$=["\\8\\3\\4\\3\\2\\2\\1\\3\\2\\3\\3\\2\\2\\7\\3\\1\\4\\1\\3\\2\\1\\3\\1\\3\\2\\2\\2\\1\\3\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\2\\1\\3\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\2\\1\\3\\1\\3\\2\\2"];6 c(){e["\\f\\g\\d\\a\\b"](5$[0])}',17,17,'|x2b|x5d|x5b|x21|_|function|x29|x28|var|x72|x74|O0|x65|window|x61|x6c'.split('|'),0,{}));
And I unpacked the following code(except 'var password = '):
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('9 5$=["\\8\\3\\4\\3\\2\\2\\1\\3\\2\\3\\3\\2\\2\\7\\3\\1\\4\\1\\3\\2\\1\\3\\1\\3\\2\\2\\2\\1\\3\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\2\\1\\3\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\1\\4\\1\\3\\2\\2\\1\\3\\1\\3\\2\\2"];6 c(){e["\\f\\g\\d\\a\\b"](5$[0])}',17,17,'|x2b|x5d|x5b|x21|_|function|x29|x28|var|x72|x74|O0|x65|window|x61|x6c'.split('|'),0,{}));
Then I got:
var _$ = ["\x28\x5b\x21\x5b\x5d\x5d\x2b\x5b\x5d\x5b\x5b\x5d\x5d\x29\x5b\x2b\x21\x2b\x5b\x5d\x2b\x5b\x2b\x5b\x5d\x5d\x5d\x2b\x5b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x5d\x2b\x5b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x2b\x21\x2b\x5b\x5d\x5d\x2b\x5b\x2b\x5b\x5d\x5d"];
function O0() {
window["\x61\x6c\x65\x72\x74"](_$[0])
}
And after decoding:
var _$ = ["([![]]+[][[]])[+!+[]+[+[]]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[+[]]"];
function O0() {
window["alert"](_$[0])
}
Now I wonder how the codes execute and what is the value of password ?
Thanks so much.
The code is obfuscated and intended to permit execution of arbitrary code even if the script text is passed through filters.
The approach is often used for tracking, phishing and other undesirable activities, so I would suggest you don't try running it.
All you need to do is run this code -- not the 'window' stuff, but only the 'decode' part -- and you'll see the solution.
Here are some pointers on decoding:
the outer array brackets are a decoy
an empty array [] evaluates to 0 when used in a calculation such as +[]
!0 = 1
!+0 evaluates to true or 1, !+1 to false or 0 (this surely must be a loophole in Javascript)
.. so !+[] is simply 1.
[1]+[1] is not a valid math sum, so both arrays are converted to strings before being added up.
The above takes care of the numbers. Where does the first character come from? The first part ([![]]+[][[]]) evaluates directly to a string two constants, which add together as a string again, and the array index after it picks up a single character.

Selenium javascript approximate value

I'am experimenting with selenium IDE and i came across a problem with asserting an approximate value. I need to check a value inside an element with an id. It is numeric value with comma (",") as a separator.
Problem is that i need to check if the numeric value is valid with a tolerance of 0.01.
For example:
<div id="uniqueId">2,54</div>
assertText - value = 2.53
I need above example to pass the test, and also pass if the value in div si 2,52 or 2,53. I understand that i can use assertEval to insert javascript, but i'm not very good in javascript and also from what i've read the javascript capabilities of selenium are limited.
Any help would be greatly appreciated!
Using assertEval is a good idea. The javascript you will need will be something like
var numberStr = "${actualText}".replace(",", ".");
var number = parseFloat(numberStr);
var difference = Math.abs(eval(number-${expectedValue}));
(difference <= 0.01)?true:false;
I don't know much javascript but according to this thread we need to first replace decimal mark from ',' to '.' (1st line) so we can later convert the string found on page to number (2nd line).
${actualText} is a variable in which we store the actual value taken from page while the ${expectedValue} is a value you need to define on your own. Note that tolerance (0.01) is "hardcoded", you may want to replace it with variable too.
Now to make it shorter (and less readable):
(Math.abs(eval(parseFloat("${actualText}".replace(",", "."))-${expectedValue}))<=0.01)?true:false
Having the javascript we can prepare Selenium script:
storeText | id=uniqueId | actualText
store | 2.53 | expectedValue
assertEval | JS LINE FORM ABOVE GOES HERE | true

Java Script equivalent to Excel Formula

Hi hope someone can assist.
I have the following formula in a excel spreadsheet. It calculates the difference between T40 and AB40, if it returns a negative value, it recalculates AB40 - T40 to return a positive value.
=IF(T40 > AB40, + T40 - AB40, AB40 - T40)
I have imported the spreadsheet to a PDF document, and cannot find an equivalent java script to match above.
Can anyone assist, I am now desperate and in urgent need of the information.
You don't need an if statement you can just do:
Math.abs(T40-AB40)
To clarify this will return the absolute value of a mathematical operation so:
Math.abs(10 - 2) //returns 8
Math.abs(2 - 10) //returns 8
If'm not familiar with the specifics of javascript in pdf, but something like this should work:
if(T40>AB40){
return T40-AB40;
}else{
return AB40-T40;
}
Or use a ternary operator like user1161318 suggested.
It can be done with an if as another response shows, but if you want to treat it as an expression, you could do it this way:
T40 > AB40 ? T40-AB40 : AB40-T40;
In Javascript:
cond ? ifTrue : ifFalse
Is an alternate way of posing an if statement that allows the whole thing to be treated as an expression, and thus be part of a formula, assigned to a variable, put in the parameter list of a function, etc... unlike if. So it's perfectly legal to do things like:
alert( cond ? ifTrue : ifFalse );
var x = cond ? ifTrue : ifFalse;
And so on, whereas you can't do that with an if block. Of course, include the expression within parenthesis if there's any chance of ambiguity (eg: it's part of a bigger formula, etc...)
I think this is closer to preserving the meaning of IF in Excel.

Categories