10 small Problems Solving of JavaScript (Tricky Part of JS)

Md. Jidanul Hakim Jitu
5 min readNov 5, 2020

Problem Solving

1. Swap Variables:

Process 1.

We can swap variables in many ways. First, I will discuss one of the easiest ways. Suppose there are two glass of juice. One is Mango Juice and other is Lemon Juice. Now we want to change the glass with each other. How can we do that? We can take an empty glass and put the Mango Juice there and then we can put the Lemon Juice there. Now Lemon glass is empty. And then, we can put the Mango juice of the temporary glass in the Empty Lemon Juice glass. Okay now we have successfully finished our swapping.

Swapping

Process 2.

You can understand from the picture

Process 3.

Little bit advance but anyone can understand. What are these? We can take an array and put the value to the array elements and then assign it reversely. It’s done! We have successfully done it.

2. Let us play with luck (simple problem solving: Random Value)

We can easily shuffle numbers or can select random values by the help of JavaScript. JavaScript can randomly select 0 to 1 floating number, so we must multiply this and figure out this with a round value.

Random Value

We can also make some dice with help of loop in JavaScript or basically any Programming Language. What we have to do?

Playing with dice

First, we must take a for loop and define a variable which initial value is 1. And then we must make another condition for how many positions we want. In this case, we have chosen to make 10 playing dice. Then we must order it that it should not be stop at what it has just done, it should run serially. Then in the loop we must define the random method like before and select the random for every loop. Okay done! In every position (means i), it will run the random value.

3. Removing duplicate number from an array

To remove duplicate number from an array first we can define another array. In this case we are giving the name of another array is “unique”. Then in a loop we must set condition of initial value and length of the array and then order it to run serially. The we must set the index of the empty array by the element of main array. And then we must set a statement that, if an element that wasn’t in empty before has to be push automatically on the empty array.

Removing Duplicate number

4. Finding Out the max number of an array

To find out the max number of an array, firstly, we must set an initial max value. And then set a for loop by giving condition that the loop must run until the array finished. While the loop is running, we have to set a statement that if an element is bigger than the previous one that will be the value of max. And thus, we will get the max number of an array.

5. Arguments

Arguments is an array like object but no exact array. We cannot push, pop on it like a function. Arguments can set only when there is a function or when we trying to access anything like function.

What happens if there are just 2 parameters on a function, but we want to add more than 2 values with this? It will not run and give us an error. So, to get rid of that we can use arguments. Instead of an array, we just have to set an arguments on the for-loop condition.

arguments

6. Evenify the numbers

To make even number from odd number by multiplying it with (2) in array. In a function just we have to set an array and then make a loop and then make a condition to run the loop till last element of the array and then set the evenify method to the array elements. Then we must push the elements.

Evenify

7. Feet to mile converter

To convert feet to mile we just must know that 1-mile equals to feet divided by 5280. (1 mile = feet/5280). Then we can set a function and set the formula to the function and return the mile.

feet to mile

8. Calculate total wood for furniture

To calculate total wood for making furniture we can assign how many wood need for different furniture and can make a sum and in a function we can set the formula and return the sum.

Calculate total wood for furniture

9. Brick calculation for different height of floor

Brick calculator

To solve this type of problem first we must understand how many what is the height of each floor. Then we must set the value of the floor height with the brick value. And then we will get the total value floor height. Then we can set different statement for different height of floor. And thus, we will get the require brick numbers.

10. Finding out the tiny name

tiny name

To solve these kinds of problem, we may set a function and must in a for-loop statement assign that, if length of an element(example ‘Sakib’ is in position 0, ‘Jitu’ is in position 1) less than every friend of friend-list (in this case) length then the friend of the friend-list length will be the length of the element. And then tiny name will be the element (after assigning the ‘minName’). Then, just return the element and we will get the tiny name.

--

--

Md. Jidanul Hakim Jitu

I am a goal-oriented problem solver with energy for web development, who might want to join a group of similar engineers.