javascript - potential infinite loop when comparing with arguments.length - javascript

I am trying to write a for-loop that goes trough all the inputs of a function, compares the elements of the inputs, and outputs unique elements (in this case they will always be numbers).
Since this is a freecodecamp exercise, I get some feedback, and get the following error: Error: potential infinite loop at line 4.
If I run the loop, without the infinite loop protection I get out of memory. Can someone point out what I'm doing wrong?
function unite(arr1, arr2, arr3) {
//for all arrays
var output = arr1;
for(var x = 0; x < arguments.length; x++) {
for(var y = 0; y < arguments[x].length; y++) {
for(var i = 0; i < output.length; i++) {
if(arguments[x][y] !== output[i]) {
output.push(arguments[x][y]);
}
}
}
}
return output;
}
unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);

I see two issues with your code.
1: You are adding items to an array as you iterate through it
You set output = arr1', then start walkingarr1as part ofarguments. Then you are adding items intoarr1/output` within the loop. These additional items are traversed and more items added.
2: The unique test is flawed
The first issue may not have been so bad if you ensured the items in the output array were unique. Unfortunately there is also an issue in your test for uniqueness in the output array. The current item is added for each entry in the output array that it doesn't match. If your output were [1,2,3,4] and the item you were comparing was 2, it would get pushed onto the output 3 times because it does not match 1, 3 or 4.

Related

Two-sum Leetcode explanation, Hashmap, Javascript

Im just wondering who can explain the algorithm of this solution step by step. I dont know how hashmap works. Can you also give a basic examples using a hashmap for me to understand this algorithm. Thank you!
var twoSum = function(nums, target) {
let hash = {};
for(let i = 0; i < nums.length; i++) {
const n = nums[i];
if(hash[target - n] !== undefined) {
return [hash[target - n], i];
}
hash[n] = i;
}
return [];
}
Your code takes an array of numbers and a target number/sum. It then returns the indexes in the array for two numbers which add up to the target number/sum.
Consider an array of numbers such as [1, 2, 3] and a target of 5. Your task is to find the two numbers in this array which add to 5. One way you can approach this problem is by looping over each number in your array and asking yourself "Is there a number (which I have already seen in my array) which I can add to the current number to get my target sum?".
Well, if we loop over the example array of [1, 2, 3] we first start at index 0 with the number 1. Currently, there are no numbers which we have already seen that we can add with 1 to get our target of 5 as we haven't looped over any numbers yet.
So, so far, we have met the number 1, which was at index 0. This is stored in the hashmap (ie object) as {'1': 0}. Where the key is the number and the value (0) is the index it was seen at. The purpose of the object is to store the numbers we have seen and the indexes they appear at.
Next, the loop continues to index 1, with the current number being 2. We can now ask ourselves the question: Is there a number which I have already seen in my array that I can add to my current number of 2 to get the target sum of 5. The amount needed to add to the current number to get to the target can be obtained by doing target-currentNumber. In this case, we are currently on 2, so we need to add 3 to get to our target sum of 5. Using the hashmap/object, we can check if we have already seen the number 3. To do this, we can try and access the object 3 key by doing obj[target-currentNumber]. Currently, our object only has the key of '1', so when we try and access the 3 key you'll get undefined. This means we haven't seen the number 3 yet, so, as of now, there isn't anything we can add to 2 to get our target sum.
So now our object/hashmap looks like {'1': 0, '2': 1}, as we have seen the number 1 which was at index 0, and we have seen the number 2 which was at index 1.
Finally, we reach the last number in your array which is at index 2. Index 2 of the array holds the number 3. Now again, we ask ourselves the question: Is there a number we have already seen which we can add to 3 (our current number) to get the target sum?. The number we need to add to 3 to get our target number of 5 is 2 (obtained by doing target-currentNumber). We can now check our object to see if we have already seen a number 2 in the array. To do so we can use obj[target-currentNumber] to get the value stored at the key 2, which stores the index of 1. This means that the number 2 does exist in the array, and so we can add it to 3 to reach our target. Since the value was in the object, we can now return our findings. That being the index of where the seen number occurred, and the index of the current number.
In general, the object is used to keep track of all the previously seen numbers in your array and keep a value of the index at which the number was seen at.
Here is an example of running your code. It returns [1, 2], as the numbers at indexes 1 and 2 can be added together to give the target sum of 5:
const twoSum = function(nums, target) {
const hash = {}; // Stores seen numbers: {seenNumber: indexItOccurred}
for (let i = 0; i < nums.length; i++) { // loop through all numbers
const n = nums[i]; // grab the current number `n`.
if (hash[target - n] !== undefined) { // check if the number we need to add to `n` to reach our target has been seen:
return [hash[target - n], i]; // grab the index of the seen number, and the index of the current number
}
hash[n] = i; // update our hash to include the. number we just saw along with its index.
}
return []; // If no numbers add up to equal the `target`, we can return an empty array
}
console.log(twoSum([1, 2, 3], 5)); // [1, 2]
A solution like this might seem over-engineered. You might be wondering why you can't just look at one number in the array, and then look at all the other numbers and see if you come across a number that adds up to equal the target. A solution like that would work perfectly fine, however, it's not very efficient. If you had N numbers in your array, in the worst case (where no two numbers add up to equal your target) you would need to loop through all of these N numbers - that means you would do N iterations. However, for each iteration where you look at a singular number, you would then need to look at each other number using a inner loop. This would mean that for each iteration of your outer loop you would do N iterations of your inner loop. This would result in you doing N*N or N2 work (O(N2) work). Unlike this approach, the solution described in the first half of this answer only needs to do N iterations over the entire array. Using the object, we can find whether or not a number is in the object in constant (O(1)) time, which means that the total work for the above algorithm is only O(N).
For further information about how objects work, you can read about bracket notation and other property accessor methods here.
You may want to check out this method, it worked so well for me and I have written a lot of comments on it to help even a beginner understand better.
let nums = [2, 7, 11, 15];
let target = 9;
function twoSums(arr, t){
let num1;
//create the variable for the first number
let num2;
//create the variable for the second number
let index1;
//create the variable for the index of the first number
let index2;
//create the variable for the index of the second number
for(let i = 0; i < arr.length; i++){
//make a for loop to loop through the array elements
num1 = arr[i];
//assign the array iteration, i, value to the num1 variable
//eg: num1 = arr[0] which is 2
num2 = t - num1;
//get the difference between the target and the number in num1.
//eg: t(9) - num1(2) = 7;
if(arr.includes(num2)){
//check to see if the num2 number, 7, is contained in the array;
index1 = arr.indexOf(num2);
//if yes get the index of the num2 value, 7, from the array,
// eg: the index of 7 in the array is 1;
index2 = arr.indexOf(num1)
//get the index of the num1 value, which is 2, theindex of 2 in the array is 0;
}
}
return(`[${index1}, ${index2}]`);
//return the indexes in block parenthesis. You may choose to create an array and push the values into it, but consider space complexities.
}
console.log(twoSums(nums, target));
//call the function. Remeber we already declared the values at the top already.
//In my opinion, this method is best, it considers both time complexity and space complexityat its lowest value.
//Time complexity: 0(n)
function twoSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) {
return [numbers.indexOf(numbers[i]), numbers.lastIndexOf(numbers[j])];
}
}
}
}

Emptying array when it matches another array

So, I'm trying to match 2 different arrays. If the same cells match, I want to remove that cell from one array using the .slice method.
Edit: What I'm trying to do is remove a number from array1 if array2 contains a matching number. The way the code works now is that it only deletes 1 entry. I want all the entries deleted from the first array.
array1 = [1, 2, 4, 5, 7, 10];
array2 = [1,2,4,5,6,7,8];
var misc = function deleteValues(array, arayy) {
for(var i = 0; i < array.length; i++) {
if ( arayy[i] == array[i]) {
array.splice(i, 1);
}
}
return array;
};
I try to run this and under console log, the array1 is unchanged. It seems like the splice method isn't removing any cells. I searched SE, but couldn't find anything that could help me.
jsFiddle Demo
The problem is that you are modifying one of the arrays as you iterate, but you are still using the same index. The end result is that you end up comparing the wrong indexes to each other after the first removal. Use two indexes, have one offset back down when it removes an item, and have the other simply iterate.
var misc = function deleteValues(array, arayy) {
for(var i = 0, j = 0; i < array.length; i++, j++) {
if ( arayy[j] == array[i]) {
array.splice(i--, 1);
}
}
return array;
};
It seems you want to remove items from the first array if the values are also in the second. The reduceRight method seems suitable as it iterates from right to left over the array, hence removing items doesn't affect the index of subsequent elements in the array. The same result can be achieved with a decrementing loop.
Also, I think function declarations are better than assignment of expressions, but each to their own.
var array1 = [1, 2, 4, 5, 7, 10];
var array2 = [1,2,4,5,6,7,8];
function deleteValues(arr0, arr1) {
arr0.reduceRight(function(x, value, index) {
if (arr1.indexOf(value) != -1) {
arr0.splice(index, 1);
}
},'');
// Not necessary but handy for chaining
return arr0;
}
document.write(deleteValues(array1, array2));
Using an arrow function, the above can be reduced to:
function deleteValues(arr0, arr1) {
arr0.reduceRight((x, v, i) => arr1.indexOf(v) != -1? arr0.splice(i, 1):'');
return arr0;
}

Explain the solution of the "Reverse an array" task from the Eloquent JS book

Could anyone provide a clear explanation on what did they do in the example below? It is really confusing.
function reverseArrayInPlace(array) {
for (var i = 0; i < Math.floor(array.length / 2); i++) {
var old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
The loop iterates through the first half of the array, up to but not including the middle value for an array with an odd number of elements (since the middle value stays in the same place when the array is reversed).
The first time through the loop, the values of the first and last positions in the array are reversed.
The second time through the loop, the values of the second and second last positions in the array are reversed.
etc...
It's not very complicated really, just get a bit of paper, write down 5 boxes for the array and then step through the program in your head working out what happens at each step. For example:
1,2,3,4,5
i=0
old=1
5,2,3,4,5
5,2,3,4,1
i=1
Continue like that, I got you started.

Convert columns to rows in javascript

I have read this Swap rows with columns (transposition) of a matrix in javascript However, it did not work for me (because I still stupi).
There are numbers of arrays each as individual colum like that:
id [1, 2, 3]
caption [one, two, three]
title [One, Two, Three]
I want to convert columns to row:
arr= [1, one, One]
...
Some code
var res = [];
for(i in this.fields) {
for(j in this.fields[i].value) {
res[j][i] = this.fields[i].value[j];
}
}
it give me "TypeError: can't convert undefined to object "
In php this method works fine, but could somebody point me how to do it in js. Thanks in advance.
UPDATE for simplication
var arr = [];
arr[0] = [];
arr[6][0] = 5;
/*
Exception: can't convert undefined to object
#Scratchpad/1:4
*/
When we scan common string we iterate with indexes like 0-0, 0-1, 0-2 until end-of-row when starts again 1-0 and so on. But here I need 0-0, 1-0, 2-0 end of col and again 1-0, 1-1, 1-1 ...
UPDATE for "this". Just add a cople of lines:
console.log(this.fields[i].value[j]);
console.log('indexes: i='+i, 'j='+j);
and as could you see there are no undefined values
4
indexes: i=0 j=0
1
indexes: i=1 j=0
1
indexes: i=2 j=0
one
indexes: i=3 j=0
One
indexes: i=4 j=0
There are a few mistakes in your source code. We donĀ“t know how your this.fields value looks like, but for the sake of your code snippet let it look like that:
this.fields = [
{ value: [1, 2, 3] },
{ value: [4, 5, 6] },
{ value: [7, 8, 9] }
]
If your this.fields variable looks like that, you are not so far away from the solution. Your error message says TypeError: can't convert undefined to object, so I am guessing your variable does not look like described.
When we transform your data in a form that looks like my example, your are not so far away from the solution in your code snippet. The problem is res does not know, that its second dimension is supposed to consist of arrays, because you never defined that. We can fix that by adding if(i === 0) res[j] = [];.
So with the described structure and our little fix, it should work:
var res = [];
for(i in this.fields) {
for(j in this.fields[i].value) {
if(i === 0) res[j] = [];
res[j][i] = this.fields[i].value[j];
}
}
For sure one error is within the loop itself. On the first iteration res[j] exists, but inside the inner loop res[j][i] is not defined and will throw an error. You could fix it by checking if the element already exists:
var res = [];
for(i in this.fields) {
for(j in this.fields[i].value) {
if( !res[j] ) { res[j] = []; }
res[j][i] = this.fields[i].value[j];
}
}

Why does the splice method have this strange behavior?

In my application I need to delete an element from an array. However I am new to JS. I searched the web and every blog post was talking about splice() method. So I considered to used it, but it has a very strange behavior.
Here the post I found :
http://www.w3schools.com/jsref/jsref_splice.asp
http://viralpatel.net/blogs/javascript-array-remove-element-js-array-delete-element/
Here is my Test:
it("should delete all elements in array", function () {
var ary = new Array();
for (i = 0; i < 10; i++) {
ary[i] = Math.random();
}
expect(ary.length).toBe(10);
for (i = 0; i < 10; i++) {
ary.splice(i, 1);
}
expect(ary.length).toBe(0);
});
And here is the result of the test:
Firefox 15.0.1 Linux: Run 7 tests (Passed: 6; Fails: 1; Errors 0) (44.00 ms)
should delete all elements in array failed (5.00 ms): Error: Expected 5 to be 0.
I use angular JS.
Thanks very much for replies. Here is another test that just don't pass:
var ary = new Array();
ary = ['a', 'b', 'c', 'd'];
ary.splice(0, 1);
ary.splice(1, 1);
ary.splice(2, 1);
ary.splice(3, 1);
expect(ary.length).toBe(0);
Firefox 15.0.1 Linux: Run 7 tests (Passed: 6; Fails: 1; Errors 0) (49.00 ms)
Posting server policy.should delete all elements in array failed (5.00 ms): Error: Expected 2 to be 0.
as #Matteo Tassinari suggest this should one should delete all elements right ??
The reason is simple: with each new splice your array will become shorter:
var arr = [1, 2, 3];
arr.splice(0, 1);
console.log(arr[0]); // 2
console.log(arr[2]); // undefined
console.log(arr.length); // 2
In the second loop of your code splice will change your array only five times. But the sixth time (when i will be equal to 5), arr.splice(5, 1) operation will effectively result in no-op, as your array will be already of 5 elements only.
You can fix it as in #MatteoTassinari answer, or just use splice(0, 1) (or just shift()) instead of splice(i, 1).
For that to work, you'd need to always remove the element at index 0. Otherwise, after, say, 8 elements, you'd be doing ary.splice(8, 1), and given that at this point, the array only has 2 elements left, arr.splice(8, 1) won't remove any, since the index 8 no longer exists.
for (i = 0; i < 10; i++) {
ary.splice(0, 1);
}
As you splice elements from the array, the array becomes shorter. As a result, the last 5 iterations of your loop attempt to splice elements that do not exist.
If you change the code to this:
for (i = 0; i < 10; i++) {
ary.splice(0, 1);
}
It would work as expected by your unit test.
When you splice your array, (and remove one array element), you also "move" all the other array elements forward.
Consider this array before any splicing:
ary = [0,1,2,3,4,5,6,7,8,9]
After ary.splice(0,1), it looks like this:
ary = [1,2,3,4,5,6,7,8,9],
Notice, that the 0th index (ary[0]) is now 1, and when you proceed to do a ary.splice(1, 1), then you don't remove the first element, but actually removes the second element (being 2 in this case)
I know this is not what you're asking for, but a more efficient way to "reset" your array is to do one of these two things:
ary.length = 0;
// or:
ary = [];
Try replacing this:
for (i = 0; i < 10; i++) {
ary.splice(i, 1);
}
with this:
for (i = 0; i < 10; i++) {
ary.splice(0, 1);
}
To delete a specific element, given for example:
ary = ['a', 'b', 'c', 'd'];
if you want to delete the 'c' simply do:
ary.splice(2, 1);
In fact 2 here is the 0-based index of the element which has to be deleted.

Categories