to test some things in javascript I am building a small minesweeper.
The following code is the initialization of the two-dimension array P.field. Afterwards a number of random fields are getting filled with an x, symbolizing that there's a mine on this field.
P.field = new Array(num);
for (var i = 0; i < P.field.length; i++)
P.field[i] = new Array(num);
$.each(P.field, function(index, key) {
$.each(key, function(i, k) {
P.field[index][i] = '-';
});
});
var arr = [];
while (arr.length < 10) {
var found = false;
var randomnumber = Math.ceil(Math.random()*(num*num-1));
for (var i = 0; i < arr.length; i++)
if (arr[i] == randomnumber) { found = true; break; }
if (!found) arr[arr.length] = randomnumber;
}
for (var i = 0; i < arr.length; i++) {
P.field[ Math.floor(arr[i]/num) ][ Math.floor(arr[i]-Math.floor(arr[i]/num)*num)-1 ] = 'x';
}
However, in my algorithm for counting the mines in surrounding fields, I get the console error TypeError: P.field[(r+1)] is undefined. Every field (except of those from the last row) returns this error, which is something I can't quite understand.
P.field[rows][columns] has a length of 10 per dimension in my tests ([10][10]). When I try to get the value of P.field[9][0] to P.field[9][9] there's nothing wrong. However when I adress any smaller row, this exception kicks in (P.field[0 + 1][0], P.field[3 + 1][6], and what so ever)...
I hope someone can tell me why.
edit
More code:
onReady: function() {
$('#sweeper table').on('click', 'td', function(e) {
var row = $(this).parent().attr('class'); // Class represents the index of the array
var column = $(this).attr('class'); // Class represents the index of the array
P.openField(row, column, $(this));
});
},
openField: function(r, c, e) {
if ( P.field[r][c] == 'x' ) {
e.addClass('mine');
} else {
e.html( P.surroundingMineCount(r, c) );
e.addClass('opened');
}
},
surroundingMineCount: function(r, c) {
var count = 0;
if ( P.field[r][c-1] == 'x' ) count++;
if ( P.field[r-1][c-1] == 'x' ) count++;
if ( P.field[r][c+1] == 'x' ) count++;
if ( P.field[r-1][c] == 'x' ) count++;
if ( P.field[r-1][c+1] == 'x' ) count++;
if ( P.field[r+1][c] == 'x' ) count++;
if ( P.field[r+1][c-1] == 'x' ) count++;
return count;
},
Right now I have no validation if the r+1 or r-1 is actually a valid index for that array (I had one in, but removed it for testing). However that can't really be the error, because I even get errors in the middle of the table.
Looking at the code you've provided versus the errors you've had thrown, I'm skeptical about your suspected cause as the code used does indeed generate the correct set of arrays.
I suspect it may be a slightly simpler issue, the example generation code you've provided uses:
P.field = new Array(num);
which has P as a capital, whereas the error that you've had thrown uses it as lowercase:
TypeError: p.field[(r+1)] is undefined
Are you sure you haven't accidentally just used the incorrect case when testing?
Related
The while statement keeps throwing an error (TypeError: Cannot read property '8' of undefined) however the console.log still outputs the result that it's meant to.
var hold = [];
for (let i = 0; i < (clothes.length - 1); i++) {
let count = 0;
if (hold.length === 0) {
hold.push(clothes[i]);
} else {
console.log(clothes[i][8], hold[count][8]);
while (clothes[i][8] < hold[count][8]) {
count++;
}
hold.slice(count, 0, clothes[i]);
}
}
This part of the code increments count beyond the length of hold[]
while (clothes[i][8] < hold[count][8]){
count ++;
};
stepping through manually:
clothes[0] gets added as hold[0] by the "if hold is empty clause"
clothes[1] gets compared with hold[0] and is < so count++
clothes[1] gets compared with hold[1], but there's no hold[1], so you get the error
Add a clause in the while
while (count < hold.length && clothes[i][8] < hold[count][8]){
count ++;
};
Note the length check must be first otherwise you still get the same error (there's other ways such as break out of the while). The 2nd part of the && is only valuated if the first part is true.
You have other issues stopping a complete solution:
for (let i = 0; i < (clothes.length - 1); i++){
will loop to the length-1, so if you have 3 elements, you only get two. You need to use either
i<clothes.length
i<=(clothese.length-1)
and
hold.slice(count, 0, clothes[i]);
is not the syntax for .slice and slice returns a new array, does not change the array in place. This should be
hold.splice(count, 0, clothes[i]);
Giving an updated snippet:
var clothes = [[2],[1],[3]];
var hold = []
for (let i = 0; i < clothes.length; i++) {
var count = 0;
if (hold.length === 0) {
hold.push(clothes[i]);
} else {
while (count<hold.length && clothes[i][0] < hold[count][0]) {
count++;
};
if (count == hold.length) {
hold.push(clothes[i])
}
else {
hold.splice(count, 0, clothes[i]);
}
}
}
console.log(hold.join(","));
I want to generate keys in alphabetical order in such a way that it begins with
aaaaa
then the next would be aaaab and after reaching to aaaaz the string should be aaaba and then aaabb and so on , so that the keys generate properly
My sample JSON to be created
var keygen={aaaaa,
aaaab,
aaaac .........aaaaz,aaaba ....}
my javascript
$scope.doKeyGen=function(lastValueInJSON)
{ // Do something
}
This will work for you. JS Fiddle is here.
https://jsfiddle.net/3d789okv/7/
Make sure that you the last value you give will be hit.
Otherwise you are going to an infinite loop hell.
Also you can configure the number of letters in the call to getNext().
But make sure that you set the equal number of letters in the first value and "aaaaa" and the last value "asxas"
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
var json = [];
function getNext(charCount,lastValue){
changeIndex = charCount -1;
var newValue = "";
while (changeIndex >= 0){
if(lastValue[changeIndex] !== "z"){
var changed = lastValue[changeIndex];
var replacechanged = String.fromCharCode(changed.charCodeAt(0)+1);
newValue = lastValue.replaceAt(changeIndex,replacechanged)
for(var j=changeIndex+1; j < charCount; ++j){
newValue = newValue.replaceAt(j,"a");
}
return newValue;
}
changeIndex--;
}
}
function createJSON(lastValue){
if(!json.length){
//var startPrefix = "aaaaa";
json.push("aaaaa");
while(lastValue !== json[json.length-1]){
json.push(getNext(5,json[json.length-1]));
}
console.log(json);
}
}
createJSON("aaabz");
You need to use recursive function to generate your keys.
I've written some piece of code in this fiddle link, which generate keys as per your requirement and create JSON too.
Please note I assume small case alphabets keys only. and used 3 length string (aaa), You can use 4 length also but performance degrades.
You can change any first key in input in attached fiddle, like 'aay' then code generate next all possible keys.(aaz, aba,.....,zzz).
You can use this
function getNextKey(lastKeyCode, changeIndex)
{
var charCodes = [];
if( changeIndex == undefined )
changeIndex = lastKeyCode.length - 1;
if(changeIndex - 1 > -1 && lastKeyCode.charCodeAt(changeIndex) == 122 )
{
lastKeyCode = getNextKey(lastKeyCode, changeIndex - 1);
}
lastKeyCode.split('').forEach(function(e){charCodes.push(e.charCodeAt())});
charCodes[changeIndex] = 97 + (charCodes[changeIndex] - 96 ) % 26;
return String.fromCharCode.apply(0, charCodes);
}
//-------------------EDIT ( GENERATE KEYS LIKE THIS )------------
function generateKeys(lastKey)
{
var json = [];
var nextKey = new Array(lastKey.length + 1 ).join('a');
json.push(nextKey);
while( nextKey != lastKey )
{
json.push( (nextKey = getNextKey(nextKey)) )
}
return json;
}
//---------------------------Example----------------------------
var last = 'test';
console.log('Last Key : '+last+' | Generated key length : '+generateKeys(last).length);
I have a function here that is meant to check an element against a given array of regular expressions. The array that I am passing contains ten different regular expressions.
var regExAlphabet = /[a-z]/;
var regExNumbers = /[0-9]/;
var regExWhile = /while/;
var regExIf = /if/;
var regExElse = /else/;
var regExTrue = /true/;
var regExFalse = /false/;
var regExInt = /int/;
var regExString = /string/;
var regExBoolean = /boolean/;
var regexList = [regExAlphabet, regExNumbers, regExTrue, regExFalse,
regExInt, regExString, regExBoolean, regExWhile, regExIf, regExElse];
function loopThroughOptions(regexList, element) {
for (var i = 0; i < regexList.length; i++)
failSafe(regexList[i], element) // failSafe is defined but not shown
}
var aString = "a";
loopThroughOptions(regexList, aString);
When I run this, I am getting an uncaught typeError: cannot read property length of undefined in my loopThroughOptions function. Why is this happening? How can I fix it?
EDIT: It looks like I will need to post the failSafe function. It is quite long. Take a stab at it.
var tokenList = []; // list of discovered tokens
var substringsArray = []; // any strings that are not tokens go here
function substringsHandler(array) {
for (var i = 0; i < substringsArray.length; i++) {
for (var y = 0; y < regexList.length; y++) {
failSafe(regexList[y], substringsArray[i])
}
}
}
function findAMatch(value) {
if (value == "a")
console.log("matched a");
}
function findACharMatch(value) {
if (value == "a")
console.log("matched a");
}
function failSafe(regEx, element) {
if (regEx.test(element) && element.length > 1) { // if the token is there
var x = regEx.exec(element); // give us more information on the element
var value = x["0"]; // keep track of the value of the token
var index = x.index; // keep track of the index
var substring = value;
console.log(index);
console.log(substring.length);
console.log(element.length);
tokenList.push({
value: substring,
indexFound: index});
console.log(tokenList[0]);
if (index > 0 && index + substring.length - 1 < element.length) { // if we found a token in the middle of a string
console.log("Found token in the middle of the string.");
substringsArray.push({ // give us the half that comes before the match
value: element.substring(0, index),
indexFound: 0
});
substringsArray.push({ // give us the rest of the string that occurs after the match
value: element.substring(index + value.length),
indexFound: index + value.length
});
substringsHandler(substringsArray);
// all successful token finds get sent to tokenList to search for a match
// if nothing is found, then everything gets translated to characters or digits
} else if (index > 0 && index + substring.length - 1 == element.length) { // if there is more string to the left only
console.log("Found token on the right of the string.");
substringsArray.push({
value: element.substring(0, index), // compare these values using find a match later
indexFound: 0
})
} else if (index == 0 && substring.length < element.length) { // if there is more string to the right only
console.log("Found token on the left of the string.");
substringsArray.push({
value: element.substring(substring.length),
indexFound: substring.length
})
} else { // the token is the only input
console.log("The token consists of the entire string.");
}
} else if (regEx.test && element.length == 1) {
var x = regEx.exec(element); // give us more information on the element
var value = x["0"]; // keep track of the value of the token
var index = x.index; // keep track of the index
var substring = value;
tokenList.push({
value: value,
index: index
})
} else {
console.log("No match for regexp " + regEx + "trying the next one...");
return;
}
console.log(tokenList);
tokenList.sort(function(a, b) {
return a.indexFound - b.indexFound;
});
console.log(tokenList);
for (var i = 0; i < tokenList.length; i++) {
if (tokenList[i].value.length > 1)
findAMatch(tokenList[i].value);
else
findACharMatch(tokenList[i].value);
}
};
Ok, so I ran all of your showed code and it has an error, according to RegExp docs
If the match fails, the exec() method returns null.
So, in your code, you always take for granted that regEx.exec(element); will return an array (it supposes that the RegExp will match at least one element), which, at least in your examples, is false, and you are not handling that.
In short, the easiest way to get rid of this is by returning if x is null:
var x = regEx.exec(element);
if (!x) return // add this
Tested it, and no problem was thrown, only console output was matched a
What I am trying to achieve is to allocate a panel to the column with the least amount of panels in it, and if 2 of the columns have an equal amount of panels then I take the first column and insert it there. I have attempted to implement this, which I will show below however I want to first address the problem that I am having. I understand that my code may have many flaws, and so I am open to suggestion on how to make it better. so the error I am getting through firebug is
TypeError: columns[leastItems] is undefined
return columns[leastItems].id;
and here is my javascript code that I have implemented:
function DashboardAllocation() {
var columns = document.querySelectorAll('.col-md-4.column');
var noElemsInCol = new Array(columns.length);
//Get count of children in each column into array
for (var columnNumber = 0; columnNumber < columns.length; columnNumber++) {
noElemsInCol.push(countChildren(columns[columnNumber]));
}
//Compare all values to see if they are all the same or if the first column is 0
if (compareAllValues(noElemsInCol) || countChildren(columns[0] === 0)) {
//if it is then return the first columns id
return columns[0].id;
}
//Reference http://www.programmingsimplified.com/c/source-code/c-program-find-minimum-element-in-array
var leastItems = 1;
var minimum = countChildren(columns[0]);;
for (var i = 1; i < noElemsInCol.length; i++) {
if (noElemsInCol[i] < minimum) {
minimum = noElemsInCol[i];
leastItems = i + 1;
}
}
return columns[leastItems].id;
}
//Compares all the values in the array to check if they are equal
//Reference http://stackoverflow.com/questions/9973323/javascript-compare-3-values
function compareAllValues(a) {
for (var i = 0; i < a.length; i++) {
if (a[i] === null) { return false }
for (var j = 0; j < i; j++) {
if (a[j] !== a[i]) { return false }
}
}
return true;
}
function countChildren(Nodes) {
var childrenCount = 0;
for (var nodeType in Nodes.childNodes) {
//if the nodetype is an element then we will add one
if (nodeType == Node.ELEMENT_NODE) {
childrenCount++;
}
}
return childrenCount;
}
I have referenced the sources where I took code from and hope it helps to see where I am coming from and understand what I am trying to do
You are getting problem as an index leastItems of the array columns is out of range.
Currently You are getting error as in an array of [5] you are trying to fetch the sixth element, using leastItems variable
So either use change
var leastItems = 0;
OR, Use
return columns[leastItems -1 ].id;
I've been looking for real world examples of recursion. Remember, programming Wizards, I'm and artist and in Photoshop scripting (scriptus modus operandi) it's normally used to loop over all layers and sub layers.
I'm working on a (simple) recursion script to solve a four digit combination lock. You know, start with 1, then try 2, then 3 etc until the solution is found. To make things easy in the example the second digit is correct so we know that we don't have to change that. Also the initial state the numbers start zero, but we know there are no zeroes in the final solution.
The attempt must match the solution AND also add up to 10 in order to
be solved.
This may seem a bit stoppid, but I want to put in a two part condition of the solution, mainly because I can then apply what I've learned and write a brute force suduko solver. But you must crawl before you can ice skate...
var puzzle = [0,2,0,0]; // source
var solution = [1,2,3,4];
var s = superCopy(puzzle); // working array
drawPuzzle(s);
solvePuzzle(s, puzzle);
var total = checkTotal(s, solution);
var solution = checkSolution(s, solution);
function checkTotal(arr, source)
{
var c = 0;
// count the total
for (var i = 0; i < arr.length; i++)
{
c += arr[i];
}
if (c == 10)
{
alert("Total OK")
return true;
}
}
function checkSolution(arr, source)
{
// check the solution
for (var i in arr)
{
if (arr[i] != source[i]) return false
return true;
}
}
function solvePuzzle(arr, source)
{
for (var i = 0; i < arr.length; i++)
{
// check the source
var sourceCell = source[i];
//alert("checking source " + sourceCell)
//if it's a zero we can change it
if (arr[i] == 0)
{
cell = arr[i];
cell+=1;
if (cell > 4) cell = 0;
arr[i] = cell;
}
}
// check the solution
for (var i in arr)
{
// overflow time!
if (arr[i] != source[i]) solvePuzzle(arr, source)
else
{
alert("All done!")
}
}
}
function drawPuzzle(arr)
{
var p = "";
var c = 0;
for (var i = 0; i < arr.length; i++)
{
if (arr[i] == 0) p += "-"
else p += arr[i];
c+=1;
}
alert(p);
}
function superCopy(arr)
{
// returns a true copy of an array
tempArr = new Array();
for (var i = 0; i < arr.length; i++)
{
if (arr[i] == 0) tempArr[i] = 1 // changed, thanks Nostradamnit!
else tempArr[i] = arr[i]
}
return tempArr
}
The script is incomplete. This what I have so far, it falls over with an overflow error. Note solvePuzzle and checkTotal functions are not called because I realised that solvePuzzle needs to call itself and work out the solution...which is when I ran into overflow problems and got a bit confused.
I realise that this type of question runs dangerously close to a "fix my code" venture, so I'm prepared to put a bounty out for it. Thanks.
There are a couple of problems with your code. First up, your checkSolution function stops at the very first number that matches. You probably want it to check every number before returning true, so you should move return true outside the for loop. It only gets there if all numbers match up.
Another flaw is the superCopy function, which as Nostradamnit pointed out has a flawed condition.
Your next problem is in solvePuzzle, where you have this:
if (arr[i] == 0)
{
cell = arr[i];
cell+=1;
if (cell > 4) cell = 0;
arr[i] = cell;
}
The thing is, because arr[i] is 0 and you only add 1, cell will never be 4. So your if is never going to fire. This is where your infinite loop is: it only increments values that are zero with one, so after all zeros became one, it never goes further and you keep checking if "1111" is the solution.
Now the overflow: you shouldn't be calling solvePuzzle for every cell, this grows exponentially:
for (var i in arr)
{
// overflow time!
if (arr[i] != source[i]) solvePuzzle(arr, source)
else
{
alert("All done!")
}
}
Also, you never check the result again, so the looping never ends. You would probably want to change this to this:
if(checkTotal(arr) && checkSolution(arr, source))
{
alert("All done!");
return;
}
solvePuzzle(arr, source);
As a side note (not a bug causing thing): your checkTotal function isn't using the source parameter, so you can probably leave that out. Also, there is a rogue variable called cell in solvePuzzle, which isn't a big deal in this case, but it would be better to put var in front of it so it doesn't become global. Also, there is a sourceCell variable that never gets used.
At first glance, it seems that your superCopy function is flawed. You create an empty array, then compare its 0 index (which doesn't exist) to the input array. Is that where you are getting your "overflow"?
The problem seems to be that you call solvePuzzle(arr, source) multiple times in the same iteration:
for (var i in arr)
{
// overflow time!
if (arr[i] != source[i]) solvePuzzle(arr, source)
else
{
alert("All done!")
}
}
So you iterate over every item in the array, and if it is not equal to the source item, you call solvePuzzle again. But if you have several items that don't match, solvePuzzle will get called multiple times on the same array. And for each call, it gets called again multiple times. Therefore your function calls grow exponentially and result finally in a stack overflow.
What you probably intended to do, is the following:
var areArraysEqual = true;
for (var i in arr)
{
if (arr[i] != source[i]) {
areArraysEqual = false;
break;
}
}
if (areArraysEqual) {
alert("All done!");
} else {
solvePuzzle(arr, source);
}
I haven't looked over your solution yet, but i did write a recursive one for myself to solve the problem. Perhaps it can be of help as a guideline?
var solution = [1,2,3,4];
function checkSolution(arr) {
return solution.every(function(item, index){ return item === arr[index]; });
}
function increment(arr) {
for (var i=0; arr[i] === 9; i++) {
arr[i] = 0;
}
arr[i]++;
return arr;
}
function solvePuzzle(arr) {
if (isNaN(arr[arr.length-1])) {
return null;
}
if (checkSolution(arr)) {
return arr;
}
return solvePuzzle(increment(arr));
}
var solution = solvePuzzle([1,1,1,1]);
console.log(solution); // [1,2,3,4]
Maybe little bit off topic but I have created the easiest recursion example possible in my opinion.
http://jsfiddle.net/33yfS/
var str = "9785";
function rec(a, b)
{
var c=0;
if(b==-1)
return "";
if(a!=str.charAt(b))
return rec(a+1, b);
else
{
return rec(0,b-1)+""+a;
}
}
var ans = rec(0,str.length-1)