bigInt to base2 - javascript

I'd like to get for really big decimal values like e.g. 4.951760157141521e+27 to the matching binaryString using pure javaScript.
I'm aware that 4.951760157141521e+27 not really is a normal integer anymore what also leads to the problem that just using toString(2) does not work anymore.
print = function(i) { console.log(i) }
let myDecimalNumber = 42;
print(+myDecimalNumber.toString(2));
let myDecimalNumberBIG = 4.951760157141521e+27;
print(+myDecimalNumberBIG.toString(2));
How can I fix this? My idea was to use something like a bigInt library but it seems like currently I could not find any working solution so I would really appreciate a working example :)

Related

How to rearrange characters javascript

Found some things like in Visual Basic but not Javascript and exactly what I'm trying to do. It's a tad bit different. I'm trying to figured out how to rearrange characters in a string, it's in a for loop as well in order to cut the string in half. Now I need to rearrange that.
First I have:
12345678910111213141516
then in the for loop
12345678
I'm trying fix it so now I get
72648531
But I have to do it in a way so people can't read the code and know that there's 8 characters at this point in the string without hard work and trouble. My for loop is also jumbled up and screwy so it can't be figured out. Something like this. I really cannot post the code though.
var con = "";
for (var i = complex math that equals 0; i < complex math to equal 8; i++) {
var newStr = word[i]; // I need it to come out to the rearranged somewhere close by
var con = con+""+newStr;
}
Two commonly used techniques come to mind:
A common approach to things is by doing some XOR calculations: Look at this unrelated examples:
http://www.javascriptsource.com/passwords/xor-encryption4.html
Extracting information from page with Jsoup
You can use tools like http://www.javascriptobfuscator.com/Default.aspx to make it harder for people to figure your code.

Subtracting large integers

So. I'm trying to subtract large integers. 76561198060995608 - 76561197960265728 = 100729880 type numbers. (I'm converting a 64 bit to a 32 bit) Vbscript and JS both give 100729888.
I would love to be able to do it in vbscript, but I'm either doing something wrong with cdbl (returns 100729888) or ccur (Overflow: 'ccur' error happens) or it can't be done the way I'm trying.
I've tried implementing JS libraries (bignum, bignumber) and they also haven't returned the correct number, again, maybe because of my error. BigNumber returns 100729890.
Big number code as follows:
$(document).ready(function(){
var x = new BigNumber(76561198060995608).subtract(new BigNumber(76561197960265728))
alert(x)
})
So...what am I doing wrong? Am I making a stupid mistake? I don't feel like this should take the 6+ hours it's taken me so far.
Any suggestions or help would be greatly appreciated. Thanks!
The problem is that when you try
new BigNumber(76561198060995608)
you're still relying on the JavaScript runtime to parse and represent that number before it calls the "BigNumber" constructor. I'm pretty sure you can pass a string to that constructor:
new BigNumber("76561198060995608")
and that should give you a fighting chance.

Converting JavaScript clicking to Selenium

Apparently the task I asked about earlier (JavaScript Automated Clicking) is impossible in JavaScript, or at the very least extremely difficult.
However, I have found the documentation on Selenium and how to use it extremely uninviting and difficult to understand.
Perhaps someone could help me translate this code to Selenium or, alternatively, help me with particular elements I'm having difficulty with.
function pausecomp(ms) {
ms = ms + new Date().getTime();
while (new Date() < ms){}
}
var itemlist, totalnumber, i;
itemlist = document.getElementsByClassName("image");
totalnumber = parseInt(document.getElementById("quickNavImage").childNodes[3].firstChild.firstChild.nodeValue.replace(/[0-9]* of /, ""));
for(i = 0; i < totalnumber; i = i + 1) {
console.log(i);
itemlist[i].childNodes[1].click();
pausecomp(3000);
}
Now, I know I can get elements by Class Name in Selenium, but how do I get specific child nodes?
Likewise, how do I use regex to cut out the total number of items that needs to be clicked? It is only available in text form.
And finally, how do I iterate in Selenium?
Please note, I have no available programming environments on these computers. So I cannot use Python, C#, etc. hooks unless they can be directly imported into the Selenium IDE itself. However, the documentation is difficult for me to understand, so I don't believe that is possible.
I figured it out. Please take a look at my previous linked post. I was being an idiot. I'll put the answer up there.

Getting a NaN on a variable that is quite clearly a number

This is by far the strangest error I've ever seen.
In my program I have one variable called avgVolMix. It's a decimal variable, and is not NaN (console.log(avgVolMix) prints something like 0.3526246 to console). However, using the variable at all in an assignment statement causes it to corrupt whatever is trying to use it to NaN. Example:
console.log(avgVolMix); <- prints a working decimal
var moveRatio = 10 + avgVolMix * 10;
console.log(moveRatio); <- prints NaN
I seriously have no idea why this is happening. I've tried everything to fix it; I've converted it to a string and then back, rounded it to 2 decimal places, adding 0.0001 to it - nothing works! This is the only way I can get it "working" right now:
var temp = 0.0;
for(i = 0; i <= avgVolMix; i+=0.1)
temp = i;
This assigns a number that is close to avgVolMix to temp. However, as you can see, it's extremely bad programming. I should also note that this isn't just broken with this one variable, every variable that's associated with a library I'm using does this (I'm working on a music visualizer). Does anyone know why this might be happening?
Edit: I'm not actually able to access the code right now to test any of this stuff, and since this is a company project I'm not comfortable opening up a jsfiddle anyway. I was just wondering if anyone's ever experienced something like this. I can tell you that I got the library in question from here: http://gskinner.com/blog/archives/2011/03/music-visualizer-in-html5-js-with-source-code.html
If its showing the variable value as NaN. Then try converting the variable as parseInt(); method. Hope it works. Because I also faced such problem and solved when tried it.

Syntax error. Help with one small JS snippet :(

Hey guys. I don't know much JS, but I wanted to do some quick work with jQuery.
But I've been staring at this for about an hour and I don't understand what I missed:
<script type="text/javascript">
$('#qty_6035').change(function () {
var substractedQty, stockQty, remQty;
substractedQty = (int) $('#qty_6035').val(); // missing ; before statement
stockQty = (int) $('#orig_qty_6035').val();
$('#rem_qty_6035').html(stockQty-substractedQty);
});
</script>
jQuery library is included at the beggining of the document.
Thanks.
Use parseInt function, not (int) casting
Javascript is a dynamic language so in order to convert a string into a number you could use the parseFloat/parseInt functions:
<script type="text/javascript">
$('#qty_6035').change(function () {
var substractedQty = parseFloat($('#qty_6035').val());
var stockQty = parseFloat($('#orig_qty_6035').val());
$('#rem_qty_6035').html(stockQty - substractedQty);
});
</script>
JavaScript is not Java. int is a reserved keyword but doesn't have any functionality assigned to it, and you can't cast a value that way.
You probably want:
substractedQty = parseInt($('#qty_6035').val(), 10);
Javascript doesn't support type casting like strong typed languages (C#, Java) do. To convert the field values (which are strings) to numbers you need to use the global functions parseInt() or parseFloat().
You'll probably also want to make sure the values are parsed correctly, in case a user entered some bad input instead of a number. Use isNAN() for that.

Categories