Strife in to get help or informed about programming topics and get better at programming.

“German software engineer who helps aspiring coders to learn software development enjoyably where fun & emotions come first to learn on the fly ðŸ˜‰” – Arnold Abraham

Calculate the sum of digits of a positive integer number.

The integer 1235321

Solution

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!

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

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

Solution

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!