I have a function where I delete some parts of an Array using .splice, but when I look about console.log on it's length it hasn't changed.. and with hexdump I also saw that the "deleted" string is still there
e.g.
Sudoku[j][y] = [3, 7]
Sudoku[x][y][k] = 3
function...
Sudoku[j][y].splice(Sudoku[j][y].indexOf(Sudoku[x][y][k]), 1)
console.log(Sudoku[j][y], Sudoku[j][y].length, Hexdump.dump(Sudoku[j][y]))
= [7] 2 /*Zusammenfassung des hex:*/ 3, 7
(the value that shall be deleted comes from an other var, that's why I wrote the part with the "indexOf")
The Sudoku is a 3D Matrix: the first D ans second D are the x and y rows/columns, while the third Dimension is for the rest posibilities
What can I do, to delete the value once and for all?
because I have an IF that needs to know the length of my Arrays...
after I threw a bunch of more console.log into my code I also saw that stuff...
Sometimes...
console.log(sudoku[j][y].length, sudoku[j][y], sudoku[j][y].indexOf(sudoku[x][y][k]))
sudoku[j][y].splice(sudoku[j][y].indexOf(sudoku[x][y][k]), 1);
console.log(sudoku[j][y].length, sudoku[j][y])
Results into:
4 [7, 9] 0
3 [7, 9]
so my newest try was to use an new method instead of splice:
sudoku[x][j][(sudoku[x][j].indexOf(sudoku[x][y][k]))]=sudoku[x][j][sudoku[x][j][sudoku[x][y].length-1]]
sudoku[x][j].length--
It worked in jsfiddle but ain't solved my problems in my real code...
sometimes I saw a "undefined" in my code.. but the bigger problem was that it also left the hexdumps there... so that the lenght, even after I directly said him to get smaller, hasn't changed...
You're splicing an element that is outside of the array:
var arr = [1,2,3];
arr.splice(3,1);//doesn't take anything out of the array
arr.length===3;//true
Related
While looking over new changes to JavaScript I noticed that Set and Map use .size instead of .length like arrays would.
This seems like a pointless diversion from what's normal with arrays - just one more thing to remember.
Was there a good design reason for this?
There's a lot of discussion in the esdiscuss thread "Set length property". This was a hotly debated issue, so it's not surprising that you do not necessarily agree with the resolution.
There is a tremendous amount of arguing about this in esdiscuss. Ultimately, the argument that prevailed (as evidenced by the fact that ES2015's Sets have size and not length) was summarized in a post by David Bruant:
...for me 'length' refers to a measurement with something like a ruler. You start at 0 and see up to where it goes. This is very accurate for an array which is an indexed set (starting at 0 and growing) and for arrays as considered in C (continuous sequence of bytes) which ECMAScript arrays seem inspired of. This is probably less relevant for unordered collections such as sets which I'd tend to consider as a messy bag.
And further discussed in a post by Dean Landolt:
Just wanted to jump in and say non-writable length is consistent with String behavior as well, but David makes a good point about length implying metric topology. David's suggestion of count is nice. ISTM what we're talking about is cardinality, but no need to get too silly w/ precision. Though size is just fine with me, and has plenty of prior art.
While apsillers' Jan 27, 2016 answer adds great links, a code example is missing. The size of a set is a read-only getter while that's not the case for arrays which allow modifying the length to truncate the array.
let arr = [1, 2, 3, 4]
arr.length = 2
console.log("modified length array", arr) // [1, 2]
let mySet = new Set([1, 2, 3, 4])
mySet.length = 2
mySet.size = 2
console.log("modified length set", [...mySet]) // [1, 2, 3, 4]
let str = "1234"
str.length = 2
console.log("modified length string", str) // "1234"
I'm learning about the map() method right now and I understand very basic examples.
var numbers = [2, 4, 6];
var double = numbers.map(function(value) {
return value * 2;
});
My question is, in what cases do developers use the map() method to help solve problems? Are there some good resources with real world examples?
Thanks for the help!
As #Tushar referred:
The map() method creates a new array with the results of calling a
provided function on every element in this array.
So it is basically used when you need to apply certain functionality to every single element of an array and get the result back as an array with the new results.
For example doubling the numbers:
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]
It basically helps to shorten your code eliminating the need of using for loop. But do remember it is used when every element of the array is manipulated because map() generates similar length of array provided.
For eg.- in the example you provided doubles will have [2, 8, 18].
where 2 correspond to 1.
4 correspond to 8.
9 correspond to 18.
I recommend you to watch the whole video but your answer is at the 14th minute:
Asynchronous JavaScript at Netflix by Matthew Podwysowski at JSConf Budapest 2015
Given an unknown array of integers of an unknown length, whose values are also unknown, how can I organize them into three columns, so that the sum of the left most group is the largest, the middle the second largest and the third is the smallest with the groups being as close as possible in size.
The actual goal here is to organize <ul> elements by their size (# of <li> elements they contain) into three columns. I'm looking for an answer in javascript, but if someone can explain the logic simply enough that would good enough :)
So in other words given an array such as...
var set = [1, 1, 4, 6, 7, 10, 3, 6]
Would be organized as...
var left = [10, 4]
var middle = [6, 7]
var right = [3, 6, 1, 1]
There are other possibilities. The first column sums to 14, but this could be the outcome of various combinations such as [6, 4, 3, 1]. Being organized in such a way would make it difficult to get the right values for the next column, so preferably use the largest numbers earlier on, as in my example above. *
I'm sure this has been asked and answered before but I didn't know how to look this up. I did some research and found out that this is pretty much the Partitioning Problem, although I'm still at a loss on how to do it or if there is simple one feasible answer here. Anything that works for the simple example I gave should suffice.
* EDIT: On second thought, this may be an incorrect assumption.
I'm reading "Professional JavaScript for Web Developers" (third edition) by Nicholas Zakas in an attempt to teach myself JS. However, I am having difficulty following the Location Methods section of chapter 5 on page 118 (in case you have the book). He explains that "the indexOf() method starts searching from the front of the array (item 0) and continues to the back, whereas lastIndexOf() starts from the last item in the array and continues to the front". Also he explains that "Each of these methods accepts two arguments: the item to look for and an optional index from which to start looking". He then attempts to illustrate this with examples.
As you can see below, to the right of the alert statements, he has listed what the correct output will be for each statement given the supplied argument(s). I do not understand how these outputs are determined. For example, how does alert(numbers.indexOf(4)); produce 3? I was reading this last night and thought I was just too tired to understand, however, I still cannot seem to figure out how this is achieved. I searched the Errata section from the book's companion website for a possible typo, but nothing was listed. I also searched elsewhere but found examples that mostly dealt with strings instead of numbers. Thanks for any help. This is my first post to stack overflow so my apologies if I have done something incorrect in my post.
His examples:
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4)); //3
alert(numbers.lastIndexOf(4)); //5
alert(numbers.indexOf(4, 4)); //5
alert(numbers.lastIndexOf(4, 4)); //3
The way I thought the outcome would be:
alert(numbers.indexOf(4));
//the item in the array with the fourth index, or 5
alert(numbers.lastIndexOf(4));
//5 (this was only one that seemed to make sense to me) by counting back from the last value
alert(numbers.indexOf(4, 4));
//start looking at index 4, or 5, and then count right four places to end up at 1 (last item in array).
alert(numbers.lastIndexOf(4, 4));
//1, counting back to the left from the value with index 4, or 5, to reach the first value in the array.
Any help in determining the outputs based on the required argument and then how to also count from a specified value given the additional optional argument would be much appreciated. Thanks again.
In most of the Programming languages, default indexing start from 0. Therefore, you have an understanding problem. Double consider your example with index starting from 0.
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4)); //3, because 4 is at 3rd index
alert(numbers.lastIndexOf(4)); //5, because last 4 is at 5th index
alert(numbers.indexOf(4, 4)); //5, because searching will start from 4th index
alert(numbers.lastIndexOf(4, 4)); //3, because searching will start from last 3rd element.
JavasScript arrays are zero indexed, in other words, the first item has an index of zero. This is true for almost all programming languages (apart fro XPath for some odd reason!).
The indexOf function returns the index of the first item it finds that equals the supplied argument.
var numbers = [1,2,3,4,5,4,3,2,1];
var index = numbers.indexOf(4); // index is 3
alert(numbers[index]); // outputs 4
In JS or many other languages the index count of array starts with 0 so for,
var numbers = [1,2,3,4,5,4,3,2,1];
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5
numbers[5] = 4
numbers[6] = 3
numbers[7] = 2
numbers[8] = 1
It's
indexOf("SearchString");
not
indexOf(indexNumber);
That would be awfully redundant.
I have a particular array, by which I wanted to check if two values within the array equal the value passed into the function, and if the two integers do, then pass it into a new array.
I have solved this by using two backwards while loops and caching the length as a variable, which seemed to be efficient. However, someone mentioned to me there might be a way to remove the need for one of the loops and making it much more efficient and thus optimizing the BIG O notation.
Any ideas how this could be done? This is what I have...
var intArray = [1, 3, 7, 8, 10, 4, 6, 13, 0],
newArray = [],
i = intArray.length;
function arrayCheck(k) {
while(i--) {
var z = i;
while (z--) {
if (intArray[i] + intArray[z] === k) {
newArray.push(intArray[i]);
newArray.push(intArray[z]);
}
}
}
alert(newArray);
}
arrayCheck(8);
There is an algorithm that solves this problem in linear [O(n)] time. I recommend you check out this SO answer.
Also, as others have stated, marking answers as accepted will make people more likely to answer your questions. You may wish to revisit questions you've previously asked and accept any answers that deserve it.
if you check for number N, and intArray[i] = M, then you need to find a value N-M in the array.
Build an efficient tree search to find values N-M and you can solve this in O(n+logn).