Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have two big strings A and B which store a HTML page. I want to compare those variables to see if pages are exactly same, Like if(A === B) alert("same"); else alert ("different");
but it fail to do such comparation , I suppose it is that because of very long string. How to compare in javascript such long strings ?
You can try to use MD5 hashes of long strings.
MD5 (Message-Digest algorithm 5) is a widely-used cryptographic hash
function with a 128-bit hash value. MD5 has been employed in a wide
variety of security applications, and is also commonly used to check
the integrity of data. The generated hash is also non-reversable. Data
cannot be retrieved from the message digest, the digest uniquely
identifies the data.
Try something like this :
var a = 'abasdfasfasd23141234123412';
var b = 'abasdfasfasd23141234XXXXXX';
function compare(a,b) {
var a_arr = a.split();
var b_arr = b.split();
if(a_arr.length != b_arr.length) {
return false;
} else {
for(var i = 0 ; i < a_arr.length ; i++) {
if(a_arr[i] != b_arr[i]) return false;
}
}
return true;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
https://jsfiddle.net/a/2L4t9saq/217/ is my fiddle
most of the code you can ignore, here is the function:
var modGrid = function(code){
var arr = code
console.log(arr)
for(var n=1;n<gridx+1;n++){
for(var i = 1; i<gridy+1; i++){
var garbledMess = "[x="+i+"][y="+n+"]"
var idea = arr[0]
arr.shift()
$(garbledMess).css("background-color",idea)
}
}
}
the syntax error is as follows:
Uncaught TypeError: arr.shift is not a function
at modGrid ((index):44)
at window.onload ((index):81)
since the modGrid function takes in an array (in the case of my code an array of 4 elements) the .shift() function should be removing the first option in the array, it worked before i added some more code, but now it is apparently not a function
many thanks
since the modGrid function takes in an array
It is designed to take an array, but that isn't what you are passing it.
You are passing it a string, another string, a number and another number.
modGrid('rgba(255,0,0,1)','rgba(0,255,0,1)',2,1);
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
Edit: Not a duplicate as this is not a question about using === vs ==. Both of those methods fail.
First two lines from csv:
email,Product,Term,Rate
example#gmail.com,New,24 Months,1.99%
I have tried both === and == in an if statement but it always fails. Here is the code I use. First I create the array from a csv file. Then I loop through that array. When I echo on the second to the last line the output is example#gmail.com - example#gmail.com.
This works with if(line[0] === 'New'){ until I add in the email column in my csv sheet, but now even running if(line[1] === 'New'){ does not work if the email column is there. I checked again, and by removing the email column, it works fine. Everything is formatted as text in the csv.
casper.then(function readFile() {
var stream = fs.open('ck.csv', 'r');
var line = stream.readLine();
var lineArray = [];
while(line) {
lineArray.push(line);
line = stream.readLine();
}
casper.eachThen(lineArray, function(response) {
line = response.data.split(',');
this.echo(line[0] + ' - ' + email);
if(line[0] === email && line[1] === 'New'){
Make sure both email and line[0] trimmed. (Use email = email.trim())
You can also use the localeCompare() Method
If they are equal, you'll get 0. And we all know !0 is true.
http://www.w3schools.com/jsref/jsref_localecompare.asp
if(!line[0].localeCompare(email) && !line[1].localeCompare('New')){}
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I have this function which produces the correct value when run, but I am having a hell of a time displaying the results.
Here is the JS which is calculated onChange in a form I am trying to display the resulting value elsewhere on the form. The alert displays the correct value but my id remains blank.
Thanks in advance for taking a look
function calculate_mtow1() {
togw_n = 0;
togw = $('#togw').val();
if (togw != '' && togw != 0 && togw != 'Nan') {
var togw = togw.replace(",", "");
togw_n = togw;
}
burn_n = 0;
burn = $('#burn').val();
if (burn != '' && burn !=0 && burn != 'Nan') {
var burn = burn.replace(",", "");
burn_n = burn;
}
var mtow1 = parseInt(togw_n) + parseInt(burn_n);
$('#mtow1').val(mtow1);
document.getElementById('mtow1');
alert(mtow1);
}
<td>TOW + Fuel Burn =<span id="mtow1"></span></td>
Your code is getting the element with getElementById but then not doing anything with it. You need to assign the result of getElementById to something, or call methods on it on the same line. If your goal is to put the value of mtow1 into your <span>, try doing this:
// Solution 1
var spanElement = document.getElementById("mtow1");
spanElement.innerHtml = mtow1;
Alternatively, perhaps you were trying to display the value of mtow1 by using this jQuery:
$('#mtow1').val(mtow1);
That doesn't do what you think it does. It changes the "value" attribute of the span to the value of mtow1, but that change isn't visible to the user. It's the same as writing this as your HTML:
<td>TOW + Fuel Burn =<span id="mtow1" value="valueofmtow1"></span></td>
If you want to use jQuery instead of the getElementById method I posted above, you could do this:
// Solution 2
$('#mtow1').html(mtow1);
You don't need to do both. Either solution will work on its own.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
can somebody help for my code which is written in python, i want to write it in javascript but im in trouble, i dont know how.
python code
cities={}
for line in open("linnadkaugustega.txt", "r", encoding="UTF-8"):
m=line.strip().split()
abim=[word.split(":") for word in m[1:]]
cities[m[0]]={}
for couple in abim:
cities[m[0]][couple[0]]=int(couple[1])
print(cities);
and i tried in javascript but that doesen't work
function tere(){
console.log("Tere");
$.get('read.txt', function(data) {
cities={};
var lines = (data.trim()).split();
abim=[var word.split(":") for word in m[1:]]
cities[m[0]]={};
for var couple in abim
cities[m[0]][couple[0]]=couple[1];
console.log(cities);
}, 'text');
}
tere();
can somebody help me ?
You have syntax issues translating from python to js. Heres how arrays work...
if you have an array litteral in javascript
var cities = [];
Then we would add to the array by calling push
cities.push('Portland');
...
cities.push('New York');
we can then iterate over the array by calling forEach on the array object.
cities.forEach(function (city, index){
//do work on each city
console.log(city);
});
// Portland
// New York
A few things:
.split() in JS does something different than split in python when no separator is given. To split a line into words, you'll need to split on whitespaces explicitly
you're missing the for loop over the lines of the file. Python uses the iterator syntax for reading from the file, in JavaScript an ajax request loads the whole file and you'll need to split it in lines yourself.
JavaScript does not have that m[1:] syntax, you'll need to use the .slice() method instead
JavaScript does not have array/list comprehensions. You will need to use an explicit loop, or the map method of arrays
your loop syntax is too pythonic. In JavaScript, for loops need parenthesis and an index variable.
So this should do (supposed you have the jQuery library loaded and it finds the file):
$.get('read.txt', function(data) {
var cities = {};
var lines = data.split("\n");
for (var i=0; i<lines.length; i++) {
var line = lines[i];
var m = line.trim().split(/\s+/);
var abim = m.slice(1).map(function(word) {
return word.split(":");
});
var obj = cities[m[0]] = {};
for (var j=0; j<abim.length; j++) {
var couple = abim[j];
obj[couple[0]] = couple[1];
}
}
console.log(cities);
}, 'text');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I obfuscated my javascript code using this one http://www.javascriptobfuscator.com/Default.aspx but it seems I can' find way to back it to original code.. is there any way?
You can get back some of your code, but almost all function names will be lost and local variables tend to get replaced.
Take for example the default code at the link you provided:
ORIGINAL
function NewObject(prefix)
{
var count=0;
this.SayHello=function(msg)
{
count++;
alert(prefix+msg);
}
this.GetCount=function()
{
return count;
}
}
var obj=new NewObject("Message : ");
obj.SayHello("You are welcome.");
OBFUSCATED
var _0x5601=["\x53\x61\x79\x48\x65\x6C\x6C\x6F","\x47\x65\x74\x43\x6F\x75\x6E\x74","\x4D\x65\x73\x73\x61\x67\x65\x20\x3A\x20","\x59\x6F\x75\x20\x61\x72\x65\x20\x77\x65\x6C\x63\x6F\x6D\x65\x2E"];function NewObject(_0xa158x2){var _0xa158x3=0;this[_0x5601[0]]=function (_0xa158x4){_0xa158x3++;alert(_0xa158x2+_0xa158x4);} ;this[_0x5601[1]]=function (){return _0xa158x3;} ;} ;var obj= new NewObject(_0x5601[2]);obj.SayHello(_0x5601[3]);
STEP 1 - Decode variable array
Firstly we need to decode the variable array (the part that starts var _0x5601= and ends just before the first function). I find the easiest way to do this is to copy and paste the array into Chromes developer console. Just paste the whole line and hit enter, then in the console type the variable name and you'll get something like this:
["SayHello", "GetCount", "Message : ", "You are welcome."]
STEP 2 - String Replace code for variable array item
Next we employ the help of whichever programming language you'd like to parse this new array back into the js. In essence, take your newly decoded array, and perform a string replace the rest of the code. I had PHP handy, so i did this:
<?php
// decoded array
$_0x5601 = array("SayHello", "GetCount", "Message : ", "You are welcome.");
// rest of the obfuscated code
$code = "function NewObject(_0xa158x2){var _0xa158x3=0;this[_0x5601[0]]=function (_0xa158x4){_0xa158x3++;alert(_0xa158x2+_0xa158x4);} ;this[_0x5601[1]]=function (){return _0xa158x3;} ;} ;var obj= new NewObject(_0x5601[2]);obj.SayHello(_0x5601[3]);";
// loop over array
for($x = 0; $x < count($_0x5601); $x++){
// string replace on the code
$code = str_replace('_0x5601['.$x.']', '"'.$_0x5601[$x].'"', $code);
}
// output result
echo $code;
?>
STEP 3 - BEAUTIFY
Now, lets "beautify" the code using something like: http://jsbeautifier.org/
function NewObject(_0xa158x2) {
var _0xa158x3 = 0;
this["SayHello"] = function(_0xa158x4) {
_0xa158x3++;
alert(_0xa158x2 + _0xa158x4);
};
this["GetCount"] = function() {
return _0xa158x3;
};
};
var obj = new NewObject("Message : ");
obj.SayHello("You are welcome.");
STEP 4 - Regex replace array items with object notation
The last step is to perform one last replace, but this time we need to employ the help of regex. I use an IDE called Sublime Text 2 that has the ability to do find and replace regex (im sure most IDE's have this too).
The regex pattern i used looks like this \[\"([a-zA-Z0-9\-\_]+)\"\] to explain:
\[\" // code must start with ["
( // open capturing group
[a-zA-Z0-9]+ // match all characters a-zA-Z0-9 you may need to adjust this to include -, _ etc as needed
) // capture everything in this group
\"\] // code must end with "]
You want to replace anything that matches this pattern with .$1. Resulting in:
function NewObject(_0xa158x2) {
var _0xa158x3 = 0;
this.SayHello = function(_0xa158x4) {
_0xa158x3++;
alert(_0xa158x2 + _0xa158x4);
};
this.GetCount = function() {
return _0xa158x3;
};
};
var obj = new NewObject("Message : ");
obj.SayHello("You are welcome.");
It's not quite as pretty, and as i mentioned local variables have been replaced. But if you know your code it shouldnt be too difficult to understand what they are doing.