Simple Problem: Solution with JavaScript

I try to solve some beginner level problem using JavaScript. Just for practice. We will use some basic mathematical calculations, some JavaScript Math object, and loop.

  1. Ludo Dice

In the 6th century in India Ludo was created. We don’t gonna make a Ludo game system. In this game, the player decided on their next step using a six side number dice. Dice is using to pick a random number from 1 to 6. So we will generate a number between 1 to 6 using JavaScript.

First of all I take a variable named “dice”. And then I use two JavaScript Math object. Math.round and Math.random.

#Math.random will generate a random number. And then multiplied 6 with the generated random number. It will fixed the number range to 1–6.

#Math.round will make the random number round. With out this Math.round the result will look like 3.3751720403520498 this. after using Math.round the number look like 3. You can use Math.ceil also.

2. Find Bigger Number

You have three numbers. And you have to find the big number or the low number. So first you need to understand the logic. Simple, we will compare those numbers with others.

## we will compare a and b (a>b) if it’s true then we will compare a with c (a>c) if it’s also true then we will determine that a is bigger. or if tis false then we will determine that b is bigger.

## Determine the first step was wrong then, start from the beginning. We will compare a and b(a>b) it’s false then we will compare b with c (b>C) if it is true then c is bigger. otherwise b is bigger.

Or we can solve this problem with JavaScript Math object. We will use Math.max object then we can also find the max value. Follow the code bellow:

3. Factorial Number

Now we find the answer to a factorial number. If the user gives us a number to find the factorial value, we will show the answer in output. We use for loop to solve this problem. SO what is a factorial number?
Factorial, in mathematics, the result of all positive integers not exactly or equivalent to a given positive integer and signified by that integer and a shout point. Along these lines, factorial seven is written 5!, which means 1 × 2 × 3 × 4 × 5 Factorial zero is characterized as equivalent to 1. We use a function called factorial, a for loop.

4. Factorial Number With Recursive Function

if we see this image we see the formula of factorial. And with this way we can find the factorial number of any number. In this formula we see that a random number, imagine this number is “n” . The factorial of any number is that number occasions the factorial of (that number minus 1).
So, 5*(5–1)*(5–2)*(5–3)*(5–4) = 120.

5. Reverse String

A few months ago I meet two twin brothers. And I noticed that when they talk with each other they use some horrible language. That’s not Bengali, not English, or any other language. With a very curious mind, I asked them which type of language they used while talking. They completely avoid that topic. After a few week, I catch their formula to communicate. And I realize that they reversing their word while talking. Like “Hello Sir” they say “riS olleH”. Kinde interesting. We can do this in programming. Just we use twin brother formula. “Reverse The String”.

6. Who is Elder?

Suppose we have an array containing a few people’s age. Now We have to find the oldest one. We will use for loop again to solve this problem.

7. Sum of all number in array

We have an array full of some random number. We will find the sum of all this numbers in array.

We use a function called getArraySum. Using a for loop. The initial value will 0, and this loop will go through whole array. The initial value will increase 1 in every execution. After every execution the number will store element variable. and after storing the value of array, it will add with previously stored number. This process will continue until the array end.

8. Swap the value

We have two bowl full of food. Now Our target is transfer the food bowl. Means, food in bowl A will go to bowl B, and food in bowl B will go to bowl A. We can’t do this transformation without a third bowl. So, we have bowl C. First, we take food of bowl A and move it to bowl C. Then, we move food of bowl B to bowl A. Now bowl B is empty, and then move food bowl of C to bowl B. Finally the food swap this successfully done. Here bowl C is temp variable.

9. Fibonacci Sequence

The Fibonacci sequence is a progression of numbers where a number is the expansion of the last two numbers, beginning with 0, and 1.

Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…

the expression is: Xn = Xn-1 + Xn-2

10. Foot to Mile Conversion

We know that,

1 foot = 0.000189394 mile. So we will build a program that give us a result that foot to mile.