JavaScript Internship to CEO Challenge #24/54

JavaScript Internship to CEO Challenge #24/54

Task of Challenge #24

 Create a function that will merge two arrays and return the result as a new array. The two arrays: [9, 8, 7, 6],  [1, 2, 3, 4] .

Solution

Explanation

The function mergeArrays has two inputs (array1 and array2). It also does create an empty array tempArray where the combined and merged array will be stored to.

This time you need two for loops to cover the merging process. You simply push all elements of the first array to the variable tempArray. When this is done, you approach the second array and push also each element into tempArray.

Once you’ve iterated over both arrays and all elements are stored into tempArray you just do the return and already have it. A merged array.

This isn’t the best method you should use. It is a good way to practice loops, but in terms of functionality, clean code, and extensibility you should take my JavaScript Course, where I introduce you also to all kinds of array methods to write maintainable and scalable code – a must-have for every JavaScript Developer.

To get a discount, just subscribe to my newsletter.

The output for the code above is:

Become a Bullet Proof Developer and Seek My Coding Adventures

Sharpen Your JavaScript Coding Skills by Creating GOOD Code

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

This image has an empty alt attribute; its file name is 1*kNCu5UMtqtrUrttE2pMtVg.png

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

JavaScript Internship to CEO Challenge #23/54

JavaScript Internship to CEO Challenge #23/54

Task of Challenge #23

Reverse a string. The string „JavaScript with ArnoldCode is the best!“

Solution

Explanation

The function reverseString creates an empty string string2 where the reversed string will be stored to. A simple for loop covers the tasks of reversing our string. It does it by decrementing a running index of i from the string.length towards 0 and pushing therefore from the last element of the passed string until the first into string2.

You can see the parallels to arrays in challenge #22. Strings behave some kind of similar to arrays.

After the for loop, the function just returns your newly reversed string of string2.

If you want to dig deeper into strings, arrays and their methods you can read the Mozilla Dev Notes or take my JavaScript Course, where I introduce you also to all kinds of array methods (there are plenty of them and they are very useful).

To get a discount, just subscribe to my newsletter.

The output for the code above is:

Become a Bullet Proof Developer and Seek My Coding Adventures

Sharpen Your JavaScript Coding Skills by Creating GOOD Code

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

This image has an empty alt attribute; its file name is 1*kNCu5UMtqtrUrttE2pMtVg.png

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

JavaScript Internship to CEO Challenge #22/54

JavaScript Internship to CEO Challenge #22/54

Task of Challenge #22

Rotate an array to the right 1 position. E.g. [1, 2, 3] -> [ 3, 1, 2 ] | [1, 2, 4, 6, 3] -> [ 3, 1, 2, 4, 6 ].

Solution

Explanation

The function reverseArray just creates an empty array ar2 where the reversed one gets into. A simple for loop covers the tasks of reversing our array. It does it by decrementing a running index of i from the array.length towards 0 and pushing therefore from the last element of the passed array until the first into ar2.

After the for loop, the function just returns your newly reversed array of ar2.

If you want to dig deeper into arrays and their methods you can read the Mozilla Dev Notes or take my JavaScript Course, where I introduce you also to all kinds of array methods (there are plenty of them and they are very useful).

To get a discount, just subscribe to my newsletter.

The output for the code above is:

Become a Bullet Proof Developer and Seek My Coding Adventures

https://arnoldcodeacademy.teachable.com/p/home

Sharpen Your JavaScript Coding Skills by Creating GOOD Code

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 #21/54

JavaScript Internship to CEO Challenge – Challenge #21/54

Task of Challenge #21

Rotate an array to the right 1 position. E.g. [1, 2, 3] -> [ 3, 1, 2 ] | [1, 2, 4, 6, 3] -> [ 3, 1, 2, 4, 6 ].

Solution

Explanation

You can achieve the challenge’s goal quite easily by using the opposites of shift() and the push() method for arrays. This time you have to use the pop() method which removes the last element of an array and returns it. If you store it now into a variable (in this case last) then we can use the unshift() method to push it as the first element of an array.

If you want to dig deeper into pop() and unshift() you can read the Mozilla Dev Notes or take my JavaScript Course, where I introduce you also to all kinds of array methods (there are plenty of them and they are very useful).

To get a discount, just subscribe to my newsletter.

The output for the code above is:

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?

Get Your JavaScript Coding To A Very Professional Level with this article

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 #20/54

JavaScript Internship to CEO Challenge – Challenge #20/54

Task of Challenge #20

Rotate an array to the left 1 position. E.g. [1, 2, 3, 4, 5] -> [ 2, 3, 4, 5, 1 ] | [1, 2, 4, 5] -> [ 2, 4, 5, 1 ].

Solution

Explanation

You can achieve the challenge’s goal quite easily by using the shift() and the push() method for arrays. The shift() method removes the first element of an array and returns it. If we store it now into a variable (in this case first) then we can use the push() method to push it as the last element of an array.

If you want to dig deeper into shift() and push() you can read the Mozilla Dev Notes or take my JavaScript Course, where I introduce you also to all kinds of array methods (there are plenty of them and they are very useful).

To get a discount, just subscribe to my newsletter.

The output for the code above is:

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?

Get Your JavaScript Coding To A Very Professional Level with this article

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 #19/54

JavaScript Internship to CEO Challenge – Challenge #19/54

Task of Challange #19

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

Solution

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?

Get Your JavaScript Coding To A Very Professional Level with this article

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