How the output has 4 items in the array? - javascript

var arr =[1,2,3,4];
var ct = arr.length;
for( var i=0;i<ct;i++){
ct--;
arr[i]+=i;
}
console.log(arr);//1,3,3,4
Explain the code, it's confusing me.

Your loop run 2 times.
First time with i = 0, first item in array increase 0 -> 1
Second time with i= 1, second item in array increase 1 -> 3
The ct decrease so that your loop can not reach third time, array item remain keep old value.

Well, you are not really changing the size of the array.
You create an array with 4 elements (length = 4)
You record the length into an integer, this is a copy of the value
Inside the loop, you decrease the value of ct, which is a copy of the length, so it does not effect the array at all
You increase the current element of the array by what i is at the time, so for the first iteration of the loop, you do 1 + 0
So you see, no changes are made to the length of the array, only the values that the array contains.

Related

Can anyone please explain to me why i = 1 and not 0 after running todos.pop();

I'm learning 'Loops' in Javascript.
Can anyone please explain to me why i = 1 and not 0 after running todos.pop();
for (var i = 0; i < todos.length; i++) {
todos.pop();
}
var i = 0 - means that on the first iteration, i will be 0
i < todos.length - means that loop will continue until i will become more or equal to todos.length. For example, if you have 5 todos, then todos.length will be 5. So, the loop will work for i = 0, 1, 2, 3 and 4. But since 5 is not less than 5 - it will not run 6th iteration.
i++ - means that i will be increased by one after each iteration. Iteration - is what you have in the body of the loop. todos.pop() in this case.
So, if you're getting i = 1 after running this code, this means that you have 1 (or 2) item(s) in your todos array, and loop iterated exactly one time. Like so:
i = 0
Is i < todos.length? = Is i < 1? = Is 0 < 1? - Yes, run loop body.
Running loop body (todos.pop()).
Run i++, now i = 1
Is i < todos.length? = Is i < 0 (since you poped item from todos, lengths is zero now)? = Is 1 < 0? - No, terminate the loop.
Let's say todos has 1 element in it. This is what would happen
You initialise i as 0
You pop one off todos in the body of the function.
It increments your iterator i by 1 (i++)
At the end of one iteration of that loop, the value of i will be 1.
Here's the thing though, because your todos.length is changing as the loop is proceeding, the loop will behave in ways you might find odd.
Say your array has 2 elements in it (I'm guessing this is where your question actually comes in).
You initialise i to 0
You pop one off the todos
todos.length is now 1 and i is also 1
The loop stops because it is not less than todos.length any more
i increments on each iteration. todos.pop() does not affect i but decrements todos.length.
Your loop will pop half of the elements in the array as each iteration i will increment but todos.length will decrement.

How does this if-else statement inside a function work?

I've been completing challenges on FreeCodeCamp and stumbled upon this solution for an algorithm. Can't comprehend how the if else statement works here.
function chunkArrayInGroups(arr, size) {
var temp = [];
var result = [];
for (var a = 0; a < arr.length; a++) {
if (a % size !== size - 1)
temp.push(arr[a]);
else {
temp.push(arr[a]);
result.push(temp);
temp = [];
}
}
if (temp.length !== 0)
result.push(temp);
return result;
}
Why is temp = [] at the end of the else block?
temp = [] means "reset the temp variable to an empty array"
in the if block, the arr[a] element is pushed at the end in the temparray.
in the else block, the same happens AND the whole current temp array is added at the end of the big result array of arrays, and the temparray is reset to the empty array.
Cannot say much more since there is not data or context written in your question. Hope this has answered your question.
The function divides an array into chunks of small arrays where the 'size' parameter defines the length of each chunks array.
The algorithm works as follows:
iterate each array element (for loop) until main_array elements index
is less than chunk array size (a % size !== size - 1), and push elements into
temporary array.
else block - push element into temp array, push temp array into results array
and create new empty temp array ();
at the end if temp array length is not 0 then push it also into results array.
that's it :)
Seems like the code tries to divide an array into chunks of smaller arrays of size size. Let's say you have a big bag full of apples and you want to reduce it to smaller apple bags each having at max 2 apples. How would you do in real life? You will go through the apples starting from first one and keep putting them in a small bag until it gets full. Then you move to another small bag and start to fill that it. Same thing is done here.
Inside if there is a condition checking if the temporary chunks have been filled and if not then keep pushing to the current temporary array. If yes then create a new array and keep pushing to it. temp=[] ensures the resetting.
It happens #Peter Pavluchenko. Let's walk through the code and see what it's doing.
the caller is specifying the size of chunks.
a % size !== size -1
% is a mod operator. so when a is divided by size what is the result? is the result not equal to size -1?
if the result of 'a' being divided by size is not equal to size -1, than we push the item to the end of our temp array.
If the result of 'a' being divided by size is equal to size - 1, we have found our first chunk! So lets' add the item to our temp array and add the temp array to our result array.
temp array
- item 1
- item 2
result array
- item
-- item 1
-- item 2
so when you do result[0], you will get another array.
chunk = result[0];
chunk[0] = the actual object you might be interested in.
After we have pushed the temp array to the end of our result array, we need to reset it so we can start collecting items for the next chunk.
The last 'if' is looking for situations like, you are in the middle of a chunk, but it's not a whole chunk as specified by the size argument. so we have some objects in our temp array that we need to add to the result array before we leave the method.
It appears that the task is to split up a given array (arr) into an array of arrays, of which each array has length size - except perhaps for the last which has any "leftovers" if the length of array is not an exact multiple of size.
The way the given solution works is to go through arr, pushing its elements into the temporary array temp. But every size steps - this corresponds to the else condition inside the for loop - the temp array will have the correct length (namely size), so then the entire temp array, as a single element, is appended to the result array, and then returned to being an empty array to start over.
This way, result ends up with the chunks of size size as required. The final if statement just adds the "leftovers" on - that is, anything left in temp. (But we check that it isn't empty, otherwise if the length was an exact multiple you'd have an unwated empty array at the end of result.)

Why do we use `length-i-1` in the inner loop of bubble sort algorithm

Using javascript, It's programmed to sort the elements in an array by asc order. I tried my best to understand why the inner loop uses length-i-1, but couldn't. Can anyone please help me understand why do we use it?
function bubbleSort(arr) {
for(let i=0; i<= arr.length; i++) {
for(let j=0; j< arr.length-i-1; j++) {
if(arr[j] > arr[j+1]) {
let lesser = arr[j+1];
arr[j+1] = arr[j];
arr[j] = lesser;
}
}
}
return arr;
}
Like Daniel said in his comment, it is because those items have already been sorted (eg. the largest element that ends up at the highest index in the first iteration)
Watch the gif below, notice how the 8 ends up on the far right, and then gets surrounded by the black box, signifying that it will not need to be moved anymore, and therefor doesn't need to be checked anymore (it is greater than length-i-1).
Not checking these already 'locked in' elements helps to increase the algorithm speed.
Gif is from: Bubble Sort on Wikipedia
Let's think about it in terms of steps:
Say you have an array of 10 elements just for the sake of an example.
1st step
i = 0, j goes from 0 to 9( = 10 - 0 - 1)
So it traverses the whole array.
Now, each time that we have that the current element is bigger than the next one we switch them (by if(arr[j] > arr[j+1])), so at the end of the first iteration, on the last position we will have the max element of the array.
2nd step
i = 1, j goes from 0 to 8( = 10 - 1 - 1)
Now, we can notice that we leave out the last (9th) position, but we know that it was already the maximum from the previous step, so it was in the correct position. At the end of this iteration we will have the 2nd maximum element on the 8th position, and the process continues..
Edit: Too slow on the Phone :P
After every outer Iteration, the i-th greatest Element is at the correct place. So after the First Iteration, the greatest Element is on the far right Side. Now that we know that, we don't have to compare this element in the next round.
After the second Iteration, the second largest element is on the far right Side -1 Position. So the two largest Elements are already sorted and we don't have to consider them in the next round.
Try your algorithm with an easy example and step through it by hand. Then you should see it clearly.

Array Length in Javascript

I am an absolute beginner to programming and Javascript and was watching one of Douglas crockford's videos where he says the following
Arrays unlike objects have a special length member
It is always 1 larger than the highest integer subscript
In this array
var a = [1,2,3,4,5,6,7]
a.length equals to 7.
So I am not quite sure what 1 larger than highest integer subscript means...? Is it just an outdated piece of info from a older version of Javascript or am i missing something ?
length equals to no of elements in the array and index starts from 0 , understand it like you are not starting with 1 instead with 0 so you will have 1 less than total elements(length). so
length = total elements
and
last index = length-1;
It just means that the length of an array is always* its largest index + 1.
Consider:
var a = [];
a[6] = 'foo';
console.log(a.length) //7
a[20] = 'bar';
console.log(a.length) //21
*actually not always, like for example when you use Array constructor with number argument: var a = new Array(5), the array is empty, but it has its length explicitely set to 5
Arrays are indexed from 0 (first element), but length is from 1 (one element) (array won't have length of -1 if there won't be no elements, the length would be 0; if there are 2 elements, second element will have the index of 1 and the length will be 2).
Indexes and lengths are different. While a JavaScript array will start at 0, the length will always be the true number. This is helpful since on a slice, the last number is NOT inclusive.

JavaScript -- trying to use a loop to delete certain items in an array, but it deletes all items

I first tried a forEach loop to delete items from an array, but later learned that you shouldn't do this. So I tried to make one with a normal for loop. This is to cycle through an array containing bullets, and delete them when they go outside the game area.
for (i = 0; i < playerBullets.length; i++) {
console.log(playerBullets[i].x);
console.log(playerBullets[i]);
if (playerBullets[i].x > 800 || playerBullets[i].x < 0 || playerBullets[i].y > 600 || playerBullets[i].y < 0 ) {
playerBullets.splice(i);
}
}
The console correctly shows [i] in full, and brings up a list of all contents of the array. However, the [i].x console log only displays one value, rather than the "x" value of each object in the array.
Then as soon as the first bullet goes out of bounds, all bullets disappear. Most frustrating, and highly ineffective for killing zombies.
I have also tried looping backwards through the loop, which seems to be the recommended way but it tells me that i is undefined.
Any ideas? I feel like I'm making a very simple error, because I'm using the same structure as the code on tutorial sites, so it "should work".
Thank you!
You should be passing two parameters to the .splice() method, the start index (which you're doing) and the number of elements to delete (which you're not doing):
playerBullets.splice(i, 1);
The reason looping backwards is a good idea is that if you remove an item from the middle of the array while looping through the array then your index i will be out of sync - the index of each item after the removed one decreases by 1 so on your next loop iteration with i++ you skip over the element after the removed one. If looping forwards you'd need to decrease i by 1 after deleting an item. If looping backwards this isn't a problem.
I'm guessing you got undefined when trying to loop backwards because you started from playerBullets.length, which is 1 more than the index of the last item. You need to start from playerBullets.length - 1 and go down to 0:
for (i = playerBullets.length - 1; i >= 0; i--) {
if (playerBullets[i].x > 800 || playerBullets[i].x < 0 || playerBullets[i].y > 600 || playerBullets[i].y < 0 ) {
playerBullets.splice(i, 1);
}
}
add another argument to splice : array.splice(i, howMany);
Here's more info on splice :
If no howMany parameter is specified , all elements after index are removed.

Categories