Questions about randomizing script - javascript

So today I got interested in learning a script that randomizes each letter/number, such as 0000-0000 and each 0 gets randomized by random letter or number. In my previous post I asked about how to do it and got the script, however; the problem I'm having now is completely understanding the code.
jQuery(document).ready(function ($) {
function randomised(len) {
return Math.floor(Math.random() * len);
}
function randomiseString(str){
var charSet = "abcdefghijklmnopqrstuvwxyz0123456789";
var _str = str.replace(/[^-]/g,function(a){
return charSet[randomised(charSet.length)]
});
return _str;
}
$('.combination').text(function(i,t){
return randomiseString(t);
});
});
So this is the code, and here are some of the stuff I'm wondering about:
var _str = str.replace(/[^-]/g,function(a){
return charSet[randomised(charSet.length)]
});
1) So this is the main part that makes the randomization. I'm now having perhaps silly thoughts but I'm wondering how can I force upper-case all the letters I've set above. I know I could just replace those letters with upper-case letters, but I'd like to know how can I do it with code. Also if I understand correctly it makes the charSet an array?
var _str = str.replace(/[^-]/g,function(a)
2) Why does the function have (a) value? Does it make any difference weather I leave it blank or not?
Thanks in advance!

Javascript doesn't require that the parameters passed to a function match the parameters expected by the function at all. This means that you can safely omit the parameters to any function if you don't need them, and in the code above, instead of
function (a) {
// code that doesn't use a
}
you could use
function () {
// ...
}

Related

A function that accepts a sentence containing blanks and punctuation and returns a string with them removed

I've started to solve this function but am stuck with how to start the function.
I think I have the variables right, I'm just not sure about how I should order my function. I know i eventually need to call return punctuaionless I just cant remember what else I need to do.
var s = " ";
var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()\s]/g,"");
function compress(){
}
If you are asking how to turn your code into a function, then you are close. Make it so your function accepts a string, do the replace on that string and return it.
function compress(s){
return s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()\s]/g,"");
}
var punctuationless = "lskdj. 2lkj&#* 2.e.kfj slkdjf asdf";
console.log(compress(punctuationless));

Understanding a malicious js file

I am trying my hand a reversing a malicious js script to find out what it does. the script is really long but there is one part I dont fully understand and hope you can help. I will only show a small part of the script with the relevant parts to help with this problem so as to avoid anyone accidentally running it.
The following line calls the function UU multiple times:
ib[0] = O(Arw,UU(1017-980)+UU(19+81)+UU(32*3)+UU(51+25)+UU(4508/98+0)+UU(671-606)+UU(1677/43+0)+UU(631-522)+UU(5*23)+UU(7719/83-0)+UU(6+93)+UU(4*23));
The function UU has the following setup:
function UU(s)
{
var Ea = ",!)Q ;Zrvz2^#HgS{I~1(O`ba'&l%$mqVCXG9#w0]d.-8W_34[kA5<n/RBDLsFN\\tpY6E7fy?oi|+\"xJ>ThUc=uKjeM:}*P";
var h=30090;
var yz=h+29060;
var mm=yz/650;
var PF=mm-60; 31
var i = Ea[L(s)](s-PF);
return i;
}
Part of the operation to get the return value "i" calls the function L:
function L(R)
{
return "\x63\x68\x61\x72A\x74";
}
Question: what is function L returning?
I believe the function L is trying to obfuscate its return value so as to make analysis harder. I am not sure if I need to convert this to ASCII or Decimal in order to accurately complete the string lookup in function UU
Those are escape characters. It is fairly easy to look up the values in the ASCII table, but you can also console.log the string to see the resulting value.
console.log("\x63\x68\x61\x72A\x74");
It evaluates to charAt.

how to get the total number of unique fucntions in a JS script

I am looking at the source of pace.js and it has a pretty long source code of about a thousand lines. You can view the source here.
I need to debug this code. Is there any tool or method in JavaScript using which one can identify how many unique functions are there in a given plugin? I found one way which is:
Paste the code in a text editor
Identify each function individually
Paste a console.log("i am so and so function").
Run the script and copy paste the result from the console in a text editor
Count the number of functions
Is there a easier way to do this?
This approach first finds all the functions in the window object. Then passes those function references to 'getInnerFunction()' which matches the function against a regular expression to detect any inner functions. Finally the count of functions is returned.
However it will not be able to detect inner functions of native function present in the browser, since they return
function FUNCTION NAME {
[native code]
}
this as the to string output.
For other cases this should work. Just call fnCount() and you will receive the number of functions present (subtract 2 from the result to exclude these 2 functions).
** Please correct me if there is any problem with the function matching regular expression.
function fnCount(){
var keys = Object.keys(window);
var property;
var count = 0;
for(var i=0;i<keys.length; i++){
property = window[keys[i]];
if(typeof(property) === 'function'){
count += getInnerFunction(property);
}
}
return count;
}
function getInnerFunction(property){
var fn = property.toString();
var fnCount = fn.match(/function.*\(.*\).*{.*/g).length;
return fnCount;
}
Open notepad++, press CTRL+F, type function, click Find all...

How do I create a function that checks if a date is a palindrome?

My question has two parts. I'm trying to check whether a date is a palindrome or not. In the following code, I continuously get the result of "Not a palindrome" even if the string is in fact a palindrome.
function isPalindrome(str){
var rev = str.reverse;
if(str === rev){
console.log("This is a palindrome.");
}
else{
console.log("Not a palindrome");
}
}
isPalindrome("2002");
The second part to my question is: if I wanted the function to take two arguments function isPalindrome(start_date, end_date)and have it check the dates between for palindrome years and then return those years chronologically, how do I do that? I'm not asking for anyone to actually do it for me. I'm just asking for an explanation on how to accomplish it.
Thanks in advance.
It could be something with the reverse function you are using. You could output the value of rev to see what's going one.
I would suggest you use this: How do you reverse a string in place in JavaScript?
I'm not familiar with any string reverse() function in anybody's native javascript implementation. But here's something I wrote a while back that does the palindrome thing, fwiw:
String.prototype.reverse = function (){
//Another way to reverse a string is to use Array's reverse:
// "this.split('').reverse().join('')";
//but that's boring, and recursion is fun!
if (this.length < 2) { return this.toString() };
return this.slice(-1) + this.slice(0,-1).reverse();
}
String.prototype.is_palindrome = function (){
return this.toString() === this.reverse();
}
This checks whether a string is a palindrome.
As for the second part of your question, I'm not sure how to do that off the top of my head. I would start by seeing what's natively available via javascript's Date object. Check MDN. You would only have to handle the years, so I'd just figure out the year range first and iterate over that, checking for palindromes along the way.
Are you stripping out the non-numeric characters first?
var strippedDate = str.replace(/[^\d]/g, "");
return strippedDate == strippedDate.reverse();

JavaScript & string length: why is this simple function slow as hell?

i'm implementing a charcounter in the UI, so a user can see how many characters are left for input.
To count, i use this simple function:
function typerCount(source, layerID)
{
outPanel = GetElementByID(layerID);
outPanel.innerHTML = source.value.length.toString();
}
source contains the field which values we want to meassure
layerID contains the element ID of the object we want to put the result in (a span or div)
outPanel is just a temporary var
If i activate this function, while typing the machine really slows down and i can see that FF is using one core at 100%. you can't write fluently because it hangs after each block of few letters.
The problem, it seems, may be the value.length() function call in the second line?
Regards
I can't tell you why it's that slow, there's just not enough code in your example to determine that. If you want to count characters in a textarea and limit input to n characters, check this jsfiddle. It's fast enough to type without obstruction.
It could be having problems with outPanel. Every time you call that function, it will look up that DOM node. If you are targeting the same DOM node, that's very expensive for the browser if it's doing that every single time you type a character.
Also, this is too verbose:
source.value.length.toString();
This is sufficient:
source.value.length;
JavaScript is dynamic. It doesn't need the conversion to a string.
I doubt your problem is with the use of innerHTML or getElementById().
I would try to isolate the problem by removing parts of the function and seeing how the cpu is used. For instance, try it all these ways:
var len;
function typerCount(source, layerID)
{
len = source.value.length;
}
function typerCount(source, layerID)
{
len = source.value.length.toString();
}
function typerCount(source, layerID)
{
outPanel = GetElementByID(layerID);
outPanel.innerHTML = "test";
}
As artyom.stv mentioned in the comments, cache the result of your GetElementByID call. Also, as a side note, what is GetElementByID doing? Is it doing anything else other than calling document.getElementById?
How would you cache this you say?
var outPanelsById = {};
function getOutPanelById(id) {
var panel = outPanelsById[id];
if (!panel) {
panel = document.getElementById(id);
outPanelsById[id] = panel;
}
return panel;
};
function typerCount(source, layerId) {
var panel = getOutPanelById(layerId);
panel.innerHTML = source.value.length.toString();
};
I'm thinking there has to be something else going on though, as even getElementById calls are extremely fast in FF.
Also, what is "source"? Is it a DOMElement? Or is it something else?

Categories

Resources