Javascript Problems – Create my own Array.prototype.reduce() method – myReduce()

In this exercise, I create my own Array.prototype.reduce() method – myReduce().  On line 3, I add a new method to the Array object which takes in a function and an initial value.  Starting on line 5, I loop through the array that calls this method.  The function called is applied to each element, and the […]

Javascript Problems — Select only object from array

The goal of this exercise is to take an array of items: objects, arrays, numbers, strings and null, and return only true objects.  Both null and arrays are objects in Javascript, so I have to test for those specifically.  First I used the reduce method.  On line 7, I test if an item is null […]

Javascript Recursion

I’m working on some problems to help me better understand recursion.  First, I will do a function to calculate n factorial (n!). This is how Wikipedia explains factorial: In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, The example below is a good case for using a […]

Javascript Problems – Fibonacci Sequence

The Fibonacci Sequence starts with the numbers 0 and 1, the sequence starts by adding 0+1 = 1, and then adding the last two numbers so that 1 + 1 = 2, 2+1 =3, 2+3=5.  You end up with the sequence [0,1,1,2,3,5,8,13…] Option 1 – Iterative Solution For my first attempt, I made an iterative […]

Javascript Problem – Find Vowels in String

The purpose of this exercise is to count the number of vowels in a string.  This is actually pretty easy using a regex expression.  I use the match() method to pick out the vowels [aeiou].  The “g” in “/gi” tells the expression to match all occurrences, not just the first one.  The “i” tells it […]