JavaScript Permutations using Recursion - javascript

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>");

Related

Can J2V8 execute anonymous function?

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);
}

check if number if Fibonnaci in JavaScript

Take Input from user is number is in Fibonacci series using JavaScript.
I am able to push values in array, but not able to compare
<!DOCTYPE html>
<html>
<head>
<script>
var a = new Array();
var b = prompt("Enter Number");
var i = 0,
j = 1,
k = 0,
l;
while (k < 100) {
a.push(k)
document.write(k + " ")
k = i + j;
i = j;
j = k;
}
document.write("<br>" + a[4] + a.length);
//
for (l = 0; l <= a.length; l++) {
if (a[l] == b) {
document.write(a[l]);
}
</script>
</head>
</body>
</html>
You need to use the indexOf function to find whether b is in a or not :
<script>
var a = new Array();
var b = prompt("Enter Number");
var i=0,j=1,k=0,l;
while(k < 100) {
a.push(k)
document.write(k + " ")
k = i+j;
i=j;
j=k;
}
b = parseInt(b); // Necessary for comparison
var index = a.indexOf(b);
if (index != -1) {
alert (b + ' is in position ' + index + ' of the array');
} else {
alert (b + ' is not in the array');
}
</script>

Javascript code runs fine... almost all the time

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]

Grade Statistics Calculator

I'd like to get some help on my javascript code. I made a grade statistics calculator that shows results on:
Min – Max student grade
Min – Max student average
Min – Max course grade
Min – Max course average grade
You can access it live here --> http://jsbin.com/qirefe/edit?html,css,js,output and press the "Show Results" button to see my output. (You can change the names and the grades to get a different output)
My problem is that I cannot figure out why it doesn't show the correct course names on the Min - Max course grade, although it displays the grades right. Also I cannot figure out why it calculates wrongly the min and max course average grade and displays the corresponding courses name wrong..
Any help will be very appreciated :)
The .js code:
var Course0 = Array(6);
var Course1 = Array(6);
var Course2 = Array(6);
var Student = Array(6);
var CMap = [Course0, Course1, Course2];
var NMap = ["Course0", "Course1", "Course2"];
var showResults = function () {
var Rows = document.getElementsByClassName("srow");
for (var i = 1; i < Rows.length - 1; i++) {
var values = Rows[i].getElementsByTagName("input");
Student[i - 1] = values[0].value;
for (var j = 1; j < values.length; j++) {
CMap[j - 1][i - 1] = values[j].value;
}
}
var MinID = MaxID = AvgMinID = AvgMaxID = 0;
var Min = Max = AvgMin = AvgMax = undefined;
for (var i = 0; i < Student.length; i++) {
var c0 = Course0[i];
var c1 = Course1[i];
var c2 = Course2[i];
var lessonMin = Math.min(c0, c1, c2);
var lessonMax = Math.max(c0, c1, c2);
if ((lessonMin <= Min) || (typeof Min === "undefined")) {
MinID = i;
Min = lessonMin;
}
if ((lessonMax >= Max) || (typeof Max === "undefined")) {
MaxID = i;
Max = lessonMax;
}
var Avg = Math.avg(c0, c1, c2);
if ((Avg < AvgMin) || (typeof AvgMin === "undefined")) {
AvgMinID = i;
AvgMin = Avg;
}
if ((Avg > AvgMax) || (typeof AvgMax === "undefined")) {
AvgMaxID = i;
AvgMax = Avg;
}
}
var Wrapper = document.getElementById("student-results");
Wrapper.innerHTML = "";
Wrapper.innerHTML += "<span>The Student with lower grade is: " + Student[MinID] + ", Equals To " + Min + "</span>";
Wrapper.innerHTML += "<span>The Student with higher grade is: " + Student[MaxID] + ", Equals To " + Max + "</span>";
Wrapper.innerHTML += "<hr />";
Wrapper.innerHTML += "<span>The Student with lower average grade is: " + Student[AvgMinID] + ", Equals To " + AvgMin + "</span>";
Wrapper.innerHTML += "<span>The Student with higher average grade is: " + Student[AvgMaxID] + ", Equals To " + AvgMax + "</span>";
var CourseMin = CourseMinID = CourseMax = CourseMaxID = CourseAvgMin = CourseAvgMinID = CourseAvgMax = CourseAvgMaxID = 0;
CourseMin = CourseMax = CourseAvgMin = CourseAvgMax = undefined;
for (var i = 0, j = 0; i < Student.length; i++, j += .5) {
var c0 = Course0;
var c1 = Course1;
var c2 = Course2;
var CheckMin = Math.min(c0[i], c1[i], c2[i]);
if (CourseMin > CheckMin || (typeof CourseMin === "undefined")) {
CourseMin = CheckMin;
CourseMinID = i;
}
var CheckMax = Math.max(c0[i], c1[i], c2[i]);
if (CourseMax < CheckMax || (typeof CourseMax === "undefined")) {
CourseMax = CheckMax;
CourseMaxID = parseInt(j);
}
var Avg = Math.avg(c0[i], c1[i], c2[i]);
if (Avg < CourseAvgMin || (typeof CourseAvgMin === "undefined")) {
CourseAvgMin = Avg;
CourseAvgMinID = j;
}
if (Avg > CourseAvgMax || (typeof CourseAvgMax === "undefined")) {
CourseAvgMax = Avg;
CourseAvgMaxID = parseInt(j);
}
}
console.log(CourseMaxID);
Wrapper.innerHTML += "<hr />";
Wrapper.innerHTML += "<span>The Course with lower grade have: " + NMap[CourseMinID] + ", Equals To " + CourseMin + "</span>";
Wrapper.innerHTML += "<span>The Course with higher grade have: " + NMap[CourseMaxID] + ", Equals To " + CourseMax + "</span>";
Wrapper.innerHTML += "<hr />";
Wrapper.innerHTML += "<span>The Course with lower average grade have: " + NMap[CourseAvgMinID] + ", Equals To " + CourseAvgMin + "</span>";
Wrapper.innerHTML += "<span>The Course with higher average grade have: " + NMap[CourseAvgMaxID] + ", Equals To " + CourseAvgMax + "</span>";
return null;
};
Math.avg = function () {
var Avg = 0;
var table = arguments;
for (var i = 0; i < table.length; i++) {
Avg += parseFloat(table[i]);
}
return parseFloat(Avg / table.length);
};
After examining the output of CourseMaxID and CourseMinID in the console, CourseMinID has an index of 3, but NMap only has 3 values (indexed as 0, 1, 2). So I believe this is why, for example, you are seeing:
"The Course with lower grade have: " + NMap[CourseMinID] + ", Equals To " + CourseMin; is undefined -- because the index is out of bounds.
Here's a fix for your issues with CourseMinID and CourseMaxID:
Change the definition of CourseMinID to Math.floor(j)-1;
And change CourseMaxID to be equal to Math.ceil(j);
Your call to parseInt() on a float value didn't appear to be having the intended consequence.
I'm not entirely sure why you're choosing to increment j by 0.5 each time, but from observation I noticed that for CourseMax/CourseMinID, you wanted to use the computations I noted above.
Another note, for the course average values, you are in fact outputting the student's averages. So you will want to change your logic there. It looks to me like you are giving the horizontal row of grades as parameters to the average function:
var Avg = Math.avg(c0[i], c1[i], c2[i]);
That's not what you want to parse in to Avg for course average grades. I would define another Avg function (let's call it newAvg() here) that takes as input a single array (not multiple arguments), and then call Math.Min/Math.Max on newAvg(c0), newAvg(c1), newAvg(c2).
Here's an updated jsbin link with working functionality for course averages. Overview of changes: newAvg() has been defined to take in and operate on one parameter. Keep track of indices of CourseAvgMax and CourseAvgMin. Note that I've removed some of the other operations you had earlier in this jsbin link to make it easier for me to isolate what I was working on. Hope you find it useful!

How to get the nth occurrence in a string?

I would like to get the starting position of the 2nd occurrence of ABC with something like this:
var string = "XYZ 123 ABC 456 ABC 789 ABC";
getPosition(string, 'ABC', 2) // --> 16
How would you do it?
const string = "XYZ 123 ABC 456 ABC 789 ABC";
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
console.log(
getPosition(string, 'ABC', 2) // --> 16
)
You can also use the string indexOf without creating any arrays.
The second parameter is the index to start looking for the next match.
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
var s= "XYZ 123 ABC 456 ABC 789 ABC";
nthIndex(s,'ABC',3)
/* returned value: (Number)
24
*/
Working off of kennebec's answer, I created a prototype function which will return -1 if the nth occurence is not found rather than 0.
String.prototype.nthIndexOf = function(pattern, n) {
var i = -1;
while (n-- && i++ < this.length) {
i = this.indexOf(pattern, i);
if (i < 0) break;
}
return i;
}
Because recursion is always the answer.
function getPosition(input, search, nth, curr, cnt) {
curr = curr || 0;
cnt = cnt || 0;
var index = input.indexOf(search);
if (curr === nth) {
if (~index) {
return cnt;
}
else {
return -1;
}
}
else {
if (~index) {
return getPosition(input.slice(index + search.length),
search,
nth,
++curr,
cnt + index + search.length);
}
else {
return -1;
}
}
}
Here's my solution, which just iterates over the string until n matches have been found:
String.prototype.nthIndexOf = function(searchElement, n, fromElement) {
n = n || 0;
fromElement = fromElement || 0;
while (n > 0) {
fromElement = this.indexOf(searchElement, fromElement);
if (fromElement < 0) {
return -1;
}
--n;
++fromElement;
}
return fromElement - 1;
};
var string = "XYZ 123 ABC 456 ABC 789 ABC";
console.log(string.nthIndexOf('ABC', 2));
>> 16
This method creates a function that calls for the index of nth occurrences stored in an array
function nthIndexOf(search, n) {
var myArray = [];
for(var i = 0; i < myString.length; i++) { //loop thru string to check for occurrences
if(myStr.slice(i, i + search.length) === search) { //if match found...
myArray.push(i); //store index of each occurrence
}
}
return myArray[n - 1]; //first occurrence stored in index 0
}
a simple solution just add string, character and idx:
function getCharIdx(str,char,n){
let r = 0
for (let i = 0; i<str.length; i++){
if (str[i] === char){
r++
if (r === n){
return i
}
}
}
}
Shorter way and I think easier, without creating unnecessary strings.
const findNthOccurence = (string, nth, char) => {
let index = 0
for (let i = 0; i < nth; i += 1) {
if (index !== -1) index = string.indexOf(char, index + 1)
}
return index
}
Using indexOf and Recursion:
First check if the nth position passed is greater than the total number of substring occurrences. If passed, recursively go through each index until the nth one is found.
var getNthPosition = function(str, sub, n) {
if (n > str.split(sub).length - 1) return -1;
var recursePosition = function(n) {
if (n === 0) return str.indexOf(sub);
return str.indexOf(sub, recursePosition(n - 1) + 1);
};
return recursePosition(n);
};
function getStringReminder(str, substr, occ) {
let index = str.indexOf(substr);
let preindex = '';
let i = 1;
while (index !== -1) {
preIndex = index;
if (occ == i) {
break;
}
index = str.indexOf(substr, index + 1)
i++;
}
return preIndex;
}
console.log(getStringReminder('bcdefgbcdbcd', 'bcd', 3));
I needed a function that could search from the end of the string too so I wrote this:
function getPos(str, char, index, backwards) {
var split = str.split(char);
var result = 0;
var done = false;
split.forEach(function (item, i) {
if (done) {return}
result += item.length
if (!backwards && i === index) {
done = true
return
} else if (backwards && i === split.length - index - 2) {
done = true
return
}
result += char.length
})
return result
}
Usage:
getPos('x x x', 'x', 1, false) // 2
getPos('x x x', 'x', 0, true) // 4
Using String.indexOf:
var stringToMatch = "XYZ 123 ABC 456 ABC 789 ABC";
function yetAnotherGetNthOccurance(string, seek, occurance) {
var index = 0, i = 1;
while (index !== -1) {
index = string.indexOf(seek, index + 1);
if (occurance === i) {
break;
}
i++;
}
if (index !== -1) {
console.log('Occurance found in ' + index + ' position');
}
else if (index === -1 && i !== occurance) {
console.log('Occurance not found in ' + occurance + ' position');
}
else {
console.log('Occurance not found');
}
}
yetAnotherGetNthOccurance(stringToMatch, 'ABC', 2);
// Output: Occurance found in 16 position
yetAnotherGetNthOccurance(stringToMatch, 'ABC', 20);
// Output: Occurance not found in 20 position
yetAnotherGetNthOccurance(stringToMatch, 'ZAB', 1)
// Output: Occurance not found
var getPosition = function(string, subStr, index) {
if(!string.includes(subStr)) return null;
let arrs = string.split(subStr);
if(arrs.length < index) return null;
let result = 0;
for (let i = 0; i < index; i++) {
result += arrs[i].length;
}
result += (index - 1) * subStr.length;
return result;
}
I was playing around with the following code for another question on StackOverflow and thought that it might be appropriate for here. The function printList2 allows the use of a regex and lists all the occurrences in order. (printList was an attempt at an earlier solution, but it failed in a number of cases.)
<html>
<head>
<title>Checking regex</title>
<script>
var string1 = "123xxx5yyy1234ABCxxxabc";
var search1 = /\d+/;
var search2 = /\d/;
var search3 = /abc/;
function printList(search) {
document.writeln("<p>Searching using regex: " + search + " (printList)</p>");
var list = string1.match(search);
if (list == null) {
document.writeln("<p>No matches</p>");
return;
}
// document.writeln("<p>" + list.toString() + "</p>");
// document.writeln("<p>" + typeof(list1) + "</p>");
// document.writeln("<p>" + Array.isArray(list1) + "</p>");
// document.writeln("<p>" + list1 + "</p>");
var count = list.length;
document.writeln("<ul>");
for (i = 0; i < count; i++) {
document.writeln("<li>" + " " + list[i] + " length=" + list[i].length +
" first position=" + string1.indexOf(list[i]) + "</li>");
}
document.writeln("</ul>");
}
function printList2(search) {
document.writeln("<p>Searching using regex: " + search + " (printList2)</p>");
var index = 0;
var partial = string1;
document.writeln("<ol>");
for (j = 0; j < 100; j++) {
var found = partial.match(search);
if (found == null) {
// document.writeln("<p>not found</p>");
break;
}
var size = found[0].length;
var loc = partial.search(search);
var actloc = loc + index;
document.writeln("<li>" + found[0] + " length=" + size + " first position=" + actloc);
// document.writeln(" " + partial + " " + loc);
partial = partial.substring(loc + size);
index = index + loc + size;
document.writeln("</li>");
}
document.writeln("</ol>");
}
</script>
</head>
<body>
<p>Original string is <script>document.writeln(string1);</script></p>
<script>
printList(/\d+/g);
printList2(/\d+/);
printList(/\d/g);
printList2(/\d/);
printList(/abc/g);
printList2(/abc/);
printList(/ABC/gi);
printList2(/ABC/i);
</script>
</body>
</html>

Categories