Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Saturday, August 4, 2018

Iterate with JavaScript Do...While Loops

You can run the same code multiple times by using a loop.

The next type of loop you will learn is called a "do...while" loop because it first will "do" one pass of the code inside the loop no matter what, and then it runs "while" a specified condition is true and stops once that condition is no longer true. Let's look at an example.

var ourArray = [];
var i = 0;
do {
  ourArray.push(i);
  i++;
} while (i < 5);
This behaves just as you would expect with any other type of loop, and the resulting array will look like [0, 1, 2, 3, 4]. However, what makes the do...while different from other loops is how it behaves when the condition fails on the first check. Let's see this in action.

Here is a regular while loop that will run the code in the loop as long as i < 5.

var ourArray = [];
var i = 5;
while (i < 5) {
  ourArray.push(i);
  i++;
}
Notice that we initialize the value of i to be 5. When we execute the next line, we notice that i is not less than 5. So we do not execute the code inside the loop. The result is that ourArray will end up with nothing added to it, so it will still look like this [] when all the code in the example above finishes running.

Now, take a look at a do...while loop.

var ourArray = [];
var i = 5;
do {
  ourArray.push(i);
  i++;
} while (i < 5);
In this case, we initialize the value of i as 5, just like we did with the while loop. When we get to the next line, there is no check for the value of i, so we go to the code inside the curly braces and execute it. We will add one element to the array and increment i before we get to the condition check. Then, when we get to checking if i < 5 see that i is now 6, which fails the conditional check. So we exit the loop and are done. At the end of the above example, the value of ourArray is [5].

Essentially, a do...while loop ensures that the code inside the loop will run at least once.

Let's try getting a do...while loop to work by pushing values to an array.


Change the while loop in the code to a do...while loop so that the loop will push the number 10 to myArray, and i will be equal to 11 when your code finishes running.


Tuesday, August 8, 2017

Seek & Destroy


Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5)should return 1 because it is greater than 1(index 0), but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19)should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
Remember to use Read-Search-Ask if you get stuck. Write your own code.

Monday, August 7, 2017

Falsy Bouncer




  • Remove all falsy values from an array.



  • Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.



  • Remember to use Read-Search-Ask if you get stuck. Write your own code.


Here are some helpful links:

Boolean Objects
Array.prototype.filter()  

Saturday, August 5, 2017

Slasher Flick



Return the remaining elements of an array after chopping off n elements from the head.
The head means the beginning of the array, or the zeroth index.


#perfect

Friday, August 4, 2017

freecodecamp: Chunky Monkey



Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

Thursday, July 27, 2017

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Solution:

Create to variables one for the inner array then a second array using a for loop to the largest value in each.