I'm trying to execute this piece of javascript code
(function() {
var z = '';
var b = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
for (var i = 0; i < b.length; i += 2) {
z = z + parseInt(b.substring(i, i + 2), 16) + ',';
}
z = z.substring(0, z.length - 1);
eval(eval('String.fromCharCode(' + z + ')'));
})();
but I got this error:
undefined:1: ReferenceError: document is not defined
If I assign the function to a variable, I haven't neither error nor result.
var a = function() {
var z = '';
var b = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
for (var i = 0; i < b.length; i += 2) {
z = z + parseInt(b.substring(i, i + 2), 16) + ',';
}
z = z.substring(0, z.length - 1);
eval(eval('String.fromCharCode(' + z + ')'));
};
Have you got any idea on how run this script with J2V8?
Thank you in advance
I'll be honest, I don't know what the JS is supposed to do. You have an eval wrapped in an eval, and the function has no return statement. Plus, xxxxx doesn't appear to be a valid input.
Having said all that, if I remove the wrapped eval, use a number for the the variable b and return the result, it works fine for me.
#Test
public void testExample2() {
String jsCode = "(function() {\n"
+ "var z = '';\n"
+ "var b = '12345678';\n"
+ "for (var i = 0; i < b.length; i += 2) {\n"
+ " z = z + parseInt(b.substring(i, i + 2), 16) + ',';\n"
+ "}\n"
+ "z = z.substring(0, z.length - 1);\n"
+ "return eval('String.fromCharCode(' + z + ')');\n"
+ "})();";
Object result = v8.executeScript(jsCode);
System.out.println(result);
}
Related
I know that there are many solutions on the internet for my specific question, but I have been trying to solve it in a specific way and it doesn't work and I really can't understand what is wrong.
In my case I simply want to print the permutations.
Here is my code:
a = "abc";
function f7(a, b) {
//document.write("str: "+a+" b:"+b+"<br>");
if (b.length == 2) {
perm = b + a;
return perm;
}
var c = [];
var str = [];
for (i = 0; i < a.length; i++) {
c[i] = b + a.charAt(i);
str[i] = a.substring(0, i) + a.substring(i + 1);
document.write("i: " + i + " c[i]: " + c[i] + " str[i]: " + str[i] + "<br>");
return f7(str[i], c[i]);
}
//return {str,c}
}
document.write(f7(a, ""));
//g=f7(a,"");
//document.write(g.str+"<br>");
//document.write(g.c+"<br>");
The above code doesn't go beyond the first permutation, and I can't understand why.
Thanks in advance for any advice
Returning value in the loop causes escaping the loop. You are returning the value in for statement that stops immediately before loop complete.
You can use temporary variable to save value in for loop, and then return it.
a = "abc";
function f7(a, b) {
//document.write("str: "+a+" b:"+b+"<br>");
if (b.length == 2) {
perm = b + a;
return perm;
}
var c = [];
var str = [];
var temp = '';
for (i = 0; i < a.length; i++) {
c[i] = b + a.charAt(i);
str[i] = a.substring(0, i) + a.substring(i + 1);
document.write("i: " + i + " c[i]: " + c[i] + " str[i]: " + str[i] + "<br>");
temp += f7(str[i], c[i]);
}
return temp
}
document.write(f7(a, ""));
//g=f7(a,"");
//document.write(g.str+"<br>");
//document.write(g.c+"<br>");
How to make multiplication table without repeating reverse calculations like this xy=z yx=z? I tried to use if else with !== operator but it shows nothing. My code:
for (var x = 1; x <= 10; x++) {
for (var i = 1; i <= 10; i++) {
var result = x * i;
if (result !== result){
console.log(x + ' * ' + i + ' = ' + result);
}
else {
}
}
}
Pretty simple :
for (var x = 1; x <= 10; x++) {
for (var i = x; i <= 10; i++) {
var result = x * i;
console.log(x + ' * ' + i + ' = ' + result);
}
}
Replace i = 1 by i = x on the second line so that it starts later and ignores all the previous calculations it already did.
E.G.: When you're calculating the table 3, you can start with 3*3 as you already already did 3*1 (1*3) with table 1 and 3*2 (2*3) with table 2
You could keep track of the calculations you've already done in an hash table. If it's already in the table - skip that calculation. Something like this:
var doneCalculations = {};
for (var x = 1; x <= 10; x++) {
for (var i = 1; i <= 10; i++) {;
if (doneCalculations[i+'x'+x]) continue;
doneCalculations[x+'x'+i] = true;
var result = x * i;
console.log(x + ' * ' + i + ' = ' + result);
}
}
start the second loop with first loop variable
for (var x = 1; x <= 10; x++) {
for (var i = x; i <= 10; i++) {
var result = x * i;
console.log(x + ' * ' + i + ' = ' + result);
}
}
You want to print full multiplication table for each values of x, 1 to 10. Use memoization to avoid recalculation
In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
var doneCalculations = {};
var calculations = {};
var doneCalculations = {};
for (var x = 1; x <= 10; x++) {
for (var i = 1; i <= 10; i++) {;
if (doneCalculations[i+'x'+x]) {
result = calculations[i+'x'+x]
}
else {
doneCalculations[x+'x'+i] = true;
var result = x * i;
calculations[x+'x'+i] = result;
}
console.log(x + ' * ' + i + ' = ' + result);
}
}
console.log(calculations)
hello i'm very new to javascript so forgive me if the answer seems obvious...
this is my code which is executed at the click of a button in the body
function q() {
var A = document.getElementById("time_one").value;
var B = document.getElementById("time_two").value;
var C = document.getElementById("post_number").value;
var D = (B - A) / C;
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML = x + "<br />";
}
}
i'm pretty sure the error is in the for loop... the output is supposed to be a list of numbers between A and B. and changing the innerHTML = to innerHTML += hasn't worked either?
function q() {
var A = +document.getElementById("time_one").value;
var B = +document.getElementById("time_two").value;
var C = +document.getElementById("post_number").value;
var D = (B - A) / C;
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML += x + "<br />";
}
}
You should convert the values in int and you should use +=
With innerHTML code inside the for loop you are always setting the value with the last time iterated value. Hence, you need to update your code to
for ( var x = A; x < B; x = x + D ) {
document.getElementById("q_box").innerHTML += x + "<br />";
}
OR
var y = "";
for ( var x = A; x < B; x = x + D ) {
y += x + "<br />";
}
document.getElementById("q_box").innerHTML = y;
I will recommend you to go for 2nd option, as it is better to set the updated value at once and not to extract the value and update for each iteration.
I have come across a problem that I cannot seem to resolve. Please take a look at the code below:
<script>
function createFunctions() {
var first = ["", "", ""];
var second = ["", "", ""];
var func = ["", ""];
var sign = ["", ""];
for (i = 0; i < 3; i++) {
first[i] = (Math.round(Math.random() * 9) + 1);
second[i] = (Math.round(Math.random() * 9) + 1);
sign[i] = (Math.round(Math.random()));
if (sign[i] == "1") {
sign[i] = '+';
} else {
sign[i] = '-';
}
if (first < 2) {
func[i] = 'f(x) = x ' + sign[i] + ' ' + second[i] + '<p>';
} else {
func[i] = 'f(x) = ' + first[i] + 'x ' + sign[i] + ' ' + second[i] + '<br>';
}
}
for (i = 0; i < 3; i++) {
document.getElementById("createFunctions").innerHTML += 'Function ' + [i + 1] + ': ' + func[i];
}
//whichFunction=
findAnswers(first, second, sign);
}
function findAnswers(first, second, sign, rand) {
var num = ["", "", ""];
rand = (Math.round(Math.random() * 1));
document.getElementById("findAnswers").innerHTML = 'Which <b>one (or more)</b> of these functions holds true, when plugged in with the following <b>values of x</b>? (' + [rand + 1] + ')<br>';
for (i = 0; i < 3; i++) {
num[i] = (Math.round(Math.random() * 9));
}
for (i = 0; i < 3; i++) {
ans = 0;
if (sign[rand] == "+") {
ans = [first[rand] * num[i]] + second[rand];
} else {
ans = [first[rand] * num[i]] - second[rand];
}
document.getElementById("findAnswers").innerHTML += [i + 1] + '. You put in a ' + num[i] + ': ' + ans + '<br>';
}
}
</script>
<BODY onload=createFunctions()>
<b>A Machine Called Effex</b>
<p><input type="button" value="New Examples" onclick="history.go(0)" VALUE="Refresh"></p>
<p id="createFunctions"></p>
<p id="findAnswers"></p>
Everything works great. Except occasionally, when calculating the function, the code multiplies by x, and then simply concatenates the second value onto the first, instead of adding (or subtracting).
You should change [first[rand]*num[i]] to (first[rand]*num[i]).
The [] bracket is instantiating an array with only one value (the product of your multiplication) and then you're 'adding' the array to a number, which forces the engine to cast the array to a string and concatenate the string with the number you're 'adding'.
To illustrate, consider the code below. It also instantiates an array but casts it to a number using the unary + operator. This will result in a numerical value and not an array, so your code will work as expected.
+[first[rand]*num[i]]
To further illustrate what's happening, consider the code below. It too instantiates a single-element array, but by appending it with [0], we specify the (numerical) value of that element and so the engine is not forced to cast the array to a string when you use the + operator.
[first[rand]*num[i]][0]
I tried parseInt but i really don't know where is the right place to put it in the code :
Here's the code :
function projectTime(QuelProjet) {
var tabUsers = getUsersList();
var ss = SpreadsheetApp.getActive();
var ProjectSheet = ss.getSheetByName("Liste Projets");
for (i = 1; i < 13; i++) {
//Nombre de jours du mois
var date = new Date(i + "/01/" + year);
var numProjects = getProjectNumber();
var currentMonthSheet = getSheetByMonth(tabMonth[i - 1]);
for (l = 0; l < tabUsers.length; l++) {
var firstLine = 1 + (l + 1) * 10 - 10 + (l + 1) * numProjects - numProjects;
Logger.log("Fline " + l + " : " + firstLine);
var QuelPro = parseInt(QuelProjet);
Logger.log("Quelpro : " + QuelPro);
var nbDays = getNbJours(date);
var sommeproject = 0;
var som1 = new Array(1000);
for (k = 1; k < nbDays + 1; k++) {
var Row = parseInt(firstLine + QuelPro);
Logger.log("Row : " + Row + " QuelProjet : " + QuelPro);
var Column = k + 1;
Logger.log("Column : " + Column);
var range = currentMonthSheet.getRange(Row, Column);
som1[k] = range.getValues();
//sommeproject = sommeproject + currentMonthSheet.getRange(firstLine + QuelProjet, k + 1).getValues();
}
}
}
var sommeproject = 1;
sommeproject = sommeproject + som1;
return sommeproject;
}
The problem is in this line :
var range = currentMonthSheet.getRange(Row, Column);
QuelProjet is a argument that I put like an integer in the function* when i call it in the spreadsheet
Thanks for your time
If the error is that cannot convert NaN to class, it means that either variable Row or Column or both are not integers. Your issue comes from Row not being an integer.
Since I don't know what getProjectNumber(), I can't fully debug, but this methodology should let you debug.
Use: Logger.log(typeof(variable_name)) so that you can pinpoint where the issue is.
Log the types of the following variables after defining them: numProjects, firstLine, quelPro, and row.
the logger should log 'number' each time. When it doesn't you've found your error.
If all results are 'number' it's possible that your numbers are not whoel numbers since I don't know what getProjectNumber() returns, and the getRange() function needs whole numbers