Select Page

## JavaScript Internship to CEO Challenge – Challenge #19/54

Create a function that will return in an array the first “nPrimes” prime numbers greater than a particular number “startAt”

## Explanation

The function `getPrimes` has to accept two parameters. `nPrimes` which decides the number of prime numbers we want to get out and `startAt` which decides where we start to examine the prime numbers.

We create an empty `array` and set our runningIndex `i` equal to the `startAt` variable.

A while loop makes a continuous call to the `isPrime` function and passes in the number we want to check. This is being done as long as our array is below the `nPrimes` variable.

So once we have the predestined number of primes, we will skip the while loop and return the array with prime numbers.

I have already explained the `isPrime` function in Challenge #18.

For example if you call this function with `console.log(getPrimes(10, 100));` then the following picture is the output.

## Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

## Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

## Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code

## JavaScript Internship to CEO Challenge – Challenge #18/54

Print the first 99 prime numbers

## Explanation

The function printPrimes(nPrimes) is our looping function that will print out the number of primes we pass in. We stick to 99 for this challenge.

The next step is to set n to 0, because we want to start with the 0th prime number and set i to 2, because we know the first prime number must be 2.

Then we loop over the condition of n < nPrimes and check if the value of i is a prime number by actually calling our second function: isPrime(number).

This function is all about checking if the number is below 2; no prime number. Then if it is equal to 2; a prime number. For every other case, we apply the commonly known rules for prime numbers. Being divideable only by itself or 1.

The for loop checks if we have a maxdivisor that results in the valid integer division with the result of 0. If so, it isn’t a prime number; referring back to the commonly know rules for prime numbers.

If the loop does not return false and run through successfully, the function returns true, and the while loop of printPrimes continues.

The printout for the first 18 numbers. The list goes on until 99. The output means the 0th prime number is 2, then the 1st prime number is 3, and so on…

## Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

## Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

## Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code

## JavaScript Challenge Internship to CEO #17/54

Calculate the sum of digits of a positive integer number.

The integer 1235321

## Explanation

The solution is pretty straightforward. You need to separate the digits to loop over them and add them to a sum.

You can do it by a conversion to a string because this is nothing more than a char array under the hood.

You can take advantage of this knowledge by iterating over the array with a for-of-loop. While looping you add up the sum and finally returning the sum to the caller.

This is the printout after the sum was created.

Pro-Tip: If you want to write better code, try to accomplish this task with the reduce() and map() functions from the Array.prototype.

## Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

## Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

## Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code

## JavaScript Challenge Internship to CEO #16/54

Create a function that will return a Boolean specifying if a number is prime.

Test with 1,5,6,7,9,11,13 & 27.

## Explanation

What is a prime number in maths?

Prime numbers are special numbers, greater than 1, that have exactly two factors, themselves and 1. 19 is a prime number. It can only be divided by 1 and 19. 9 is not a prime number.

In our solution, we first check if the passed number is below 2. Then we already checked against any number below 2 being no prime number.

If the number is equal to 2, then it is a prime number and we can return true.

If any number passed both checks and we are still inside our function, then we find the maximum divisor by getting the square root of our passed number.

With a for loop, we check if the number is remainderless dividable by the iterator of i. If the remainder is 0, then we have definitely another valid calculation instead of only being able to divide a number with 1 and itself to be a prime number. Therefore, we are returning false.

Testifying the given numbers results into this printout.

## Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

## Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

## Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code

## JavaScript Challenge Internship to CEO #15/54

Create a function that will find the nth Fibonacci number using recursion.

Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … . Use nth = 14

## Explanation

The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

```Fn = Fn-1 + Fn-2

with seed values

F0 = 0 and F1 = 1

Examples:
Input  : n = 2
Output : 1

Input  : n = 9
Output : 34

The solution is a simple method that is a direct recursive implementation
mathematical recurrence relation given above.

For Input: n = 14
Output: 377

```

## Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

## Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

## Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?