# Excel ROUND function in Javascript

I've got a mathematical formula in Excel, which is the following:

I've got a mathematical formula in Excel, which is the following:

```ROUND((B2+C2)*(B55/100)/12;2)
```

Initial values:

• B2 = 1000
• C2 = 0
• B55 = 0,03

Results (t means time in months).

Here is my Javascript approach:

```(function _calculateRates() {
var singlePayment = parseInt(1000, 10),
amount = singlePayment,
monthlyPayment = parseInt(0, 10),
investTime = parseFloat(12),
rate_a = parseFloat(0.03),
rate_b = parseFloat(0.03),
investment = monthlyPayment,
interest = 0;
for (var month = 0; month < investTime; month += 1) {
investment = (month === 0) ? 0 : monthlyPayment;
```interest = Number(((amount + investment) * (rate_a / 100) / 12).toFixed(2));
amount = Number((amount + interest + investment).toFixed(2));
```
}
console.log('Result: ', amount);
})();
```

As one can see, the result is not correct.

Where can I find the Microsoft Excel algorithm for ROUND() ?

## quicksort Algorithmic in JavaScript

quicksort Algorithmic in JavaScript explain in js

if we are used array in js it peovide collection of methed that found in javaScriptas push pop form and sort() before you start the algrathmic

### What is Sorting?

it is only arranging elements in the order we want. You might have come across this in your school or college days. Like arranging numbers from smaller to greater (ascending) or from greater to smaller (descending) is what we saw till now and is called sorting.
if you in javascript and used sort this shape of the example `var items = [5,3,7,6,2,9]; console.log(items.sort()); //prints [2, 3, 5, 6, 7, 9]`

### lets start to expline the Quicksort

Quick sort follows Divide and Conquer algorithm. It is dividing elements in to smaller parts based on some condition and performing the sort operations on those divided smaller parts. Hence, it works well for large datasets.
So, here are the steps how Quick sort works in simple words.

1. First choose an element which is to be called as pivot element.
2. Next, compare all array elements with the selected pivot element and arrange them in such a way that, elements less than the pivot element are to it's left and greater than pivot is to it's right.
3.Finally, perform the same operations on left and right side elements to the pivot element.
look to this code
`````` if (arr.length == 0) {return []};
var left=[];
var right=[];
var pivot=arr;
for(var i=1;i<arr.length;i++){

if(arr[i]<pivot){left.push(arr[i])}

else {right.push(arr[i])}

}
return quickSort(left).concat(pivot, quickSort(right));

}
console.log(quickSort([5,8,2,1,9,1,0]))
``````

explaine the code
first chek if the length of array no equaleies zero than element the create 2 arr one for left and other for right select the pivot is the firest elment then create loop tom pass all elemnt in array the n check if elment more privent it will push in the right otherwise it add in the left the make call the aagin to implemantation to the leftside with connect withe right withe prive in the midalle withe the quickSort for right side untill the for loop it finsh

## Insertion Sort Algorithm in JavaScript A brief look at insertion sort written in JavaScript, including checking the loop invariant and analyzing the run&nbsp;time.

A brief look at insertion sort written in JavaScript, including checking the loop invariant and analyzing the run time.

Insertion Sort works by selecting the element at the current index, i, and searches from right-to-left for the correct place to place the item.

### Implementing Insertion Sort

Insertion sort is a very simple algorithm that works best for data that is already mostly sorted. Before getting started, it is always a good idea have a visualization of how the algorithm functions. You can refer to the video at the top of the post to get a sense for how insertion sort works. I recommend using the scrubber to slide through the animation one frame at a time and watch how the indices change.

The basic idea is that we select one element at a time and then search for the correct place to *insert it. *Hence the name, insertion sort. This results in the array having two parts — the sorted part of the array and the elements that have yet to be sorted. Some like to picture this as two different arrays — one with all of the unsorted elements and a new one that is entirely sorted. However, picturing it as one array is more true to how the code works.

Let’s take a look at the code block and and then discuss it.

``````const insertionSort = (nums) => {
for (let i = 1; i < nums.length; i++) {
let j = i - 1
let tmp = nums[i]
while (j >= 0 && nums[j] > tmp) {
nums[j + 1] = nums[j]
j--
}
nums[j+1] = tmp
}
return nums
}

``````

The code for insertion sort has two indices, `i `and `j`. `i `tracks our outer loop and represents the current element we are sorting. It starts at 1 instead of 0 because when we only have one element in our newly sorted array, there is nothing to sort. Therefore, we start at the second element and compare it against the first. The second index, `j`, starts at `i-1` and iterates from right-to-left until it finds the correct location to place the element. Along the way, we move each element over by one to make room for the new element being sorted.

And that’s all there is to it! If you are just interested in the implementation, then you do not need to read any further. But if you would like to know what makes this implementation correct, then please continue!

### Checking the Loop Invariant Conditions

In order to know for sure that our algorithm is working correctly and not just accidentally giving us the correct output for the given input, we can establish a set of conditions that must be true at the beginning of the algorithm, the end of it, and every step in-between. This set of conditions is called the loop invariant and must hold true after each loop iteration.

The loop invariant is not something that is always the same. It depends entirely on the implementation of the algorithm and is something we must determine as the designer of the algorithm. In our case, we iterate through the array one at a time, and then search right-to-left for the correct place to insert it. This results in the left part of the array, up to the current index, always being a sorted permutation of the elements originally found in that slice of the array. In other words…

The loop invariant for insertion sort states that all elements up to the current index, `<strong><em>A[0..index]</em></strong>`, make up a sorted permutation of the elements originally found in `<strong><em>A[0..index]</em></strong>` before we began sorting.
To check these conditions, we need a function that can be called from within the loop that takes in as arguments:

1. The newly sorted array
2. The original input
3. The current index.

Once we have that, we will slice the arrays from 0 to the current index and run our checks. The first check is that all elements in the new array are contained within the old array and second, that they are all in order.

``````const checkLoopInvariant = (newArr, originalArr, index) => {
//need to slice at least 1 element out
if (index < 1) index = 1

newArr = newArr.slice(0,index)
originalArr = originalArr.slice(0, index)

for (let i=0; i < newArr.length; i++) {

//check that the original array contains the value
if (!originalArr.includes(newArr[i])) {
console.error(`Failed! Original array does not include \${newArr[i]}`)
}

//check that the new array is in sorted order
if (i < newArr.length - 1 && newArr[i] > newArr[i+1]) {
console.error(`Failed! \${newArr[i]} is not less than \${newArr[i+1]}`)
}
}
}

``````

Function used to check the insertion sort loop invariant

If we call this function before, during, and after our loop and it passes without any errors, we can be confident that our algorithm is working properly. Modifying our code to include this check would look like this…

``````const insertionSort = (nums) => {
checkLoopInvariant(nums, input, 0)
for (let i = 1; i < nums.length; i++) {
...
checkLoopInvariant(nums, input, i)
while (j >= 0 && nums[j] > tmp) {
...
}
nums[j+1] = tmp
}
checkLoopInvariant(nums, input, nums.length)
return nums
}

``````

For a visual example, consider the state of our array after when the index is 2, so it has sorted 3 elements.

Our array after the first 3 indices have been processed. The loop invariant is upheld because the first 3 numbers are the same, but in sorted order.

As you can see, we have processed 3 elements and those first 3 elements are in order. You can also see that the first 3 numbers of the sorted array are the same as the first 3 numbers in the original input, just in a different order. Thus, the loop invariant is maintained.

### Analyzing the Run Time

The last thing we are going to be looking at with insertion sort is the run time. Performing a true run time analysis involves a lot of math and you can find yourself in the weeds pretty quickly. If you are interested in that type of analysis, please refer to Cormen’s Introduction to Algorithms, 3rd Edition. For the purposes of this post, however, we are only going to be performing a worst-case analysis.

The worst-case scenario for insertion sort is that the input array is in reverse-sorted order. This means that for every element that we iterate over, we will end up having to iterate backwards over every element we have already sorted to find the correct insertion point. The outer loop can be represented as a summation from 2 to n (where n is the size of the input), and each iteration has to perform `i-1` operates as it iterates from `i-1` down to zero.

Number of iterations in a worst-case insertion sort.

The proof for that summation is out of the scope of this post. I honestly just plugged it into Wolfram Alpha. Just to compare that to the best-case scenario, in which the elements are already sorted, so each iteration is constant time…

Number of iterations in a best-case insertion sort.

In terms of big-O notation, the worst-case scenario is Ɵ(n²) and the best-case is Ɵ(n). We always take the worst-case result though, so the algorithm as a whole is Ɵ(n²).

### Summary

Insertion sort works best when the input array is already mostly sorted. A good application for this is inserting a new element into an already sorted data-store. Even though you will likely never have to write your own sorting algorithms, and other sorts, such as merge sortand quick sort, are faster, I believe it’s fun to analyze algorithms in this way.

## Factorial Algorithm: ES5 & ES6 JavaScript Solutions

The factorial algorithm is one of the common&nbsp;whiteboard&nbsp;algorithms that you can come across.

The factorial algorithm is one of the common whiteboard algorithms that you can come across.

```The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.
```

That is:

```0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1
```

ES5: While method
```function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer
integer = integer - 1
}
return final
}
```

Let's break down the code:

```function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer
```

We start with assigning an initial value of 1 to variable final. Inside of the later used while method we will be able to assign the same value as an integer variable by multiplying final variable by the integer argument variable. By doing this we make sure we will start with multiplying the highest possible number by the next smallest possible number.

```  while (integer > 0) {
```

Then we run a while method where the condition is that it will loop as long as the value of integer is greater than zero.

```    final *= integer
integer = integer - 1
```

Inside of the while loop we multiply the variable final by the integervalue. The integer variable has a new value assigned in every loop execution.

```    integer = integer - 1
}
return final
```

At some point, the value of the integer variable will be 0 (because we deduct 1 from the integer value in every loop execution - integer = integer - 1), because of that the while loop will stop executing and the value of final variable will be the factorial of the value of integerargument variable we passed into the function.

Let's see an example with integer variable having a value of 3.

```integer = 3;
function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer // 1st loop: 1 * 3 = 3 | 2nd loop: 3 * 2 = 6 | 3rd loop: 6 * 1 = 6
integer = integer - 1 // 1st loop: 3 - 1 = 2 | 2nd loop: 2 - 1 = 1 | 3rd loop: 1 - 1 = 0
}
return final // 6 because of the 3rd loop of final *= integer = 6 * 1
}
```

ES6: Arrow Loop Function with Conditional (ternary) operator
```const factorial = integer => integer ? (integer * factorial(integer - 1)) : 1;
```

Here we have a one-line arrow function that takes integer as argument variable. Inside of the function is shorthand if else statement.

```/*const factorial = integer =>*/ integer ? /*(integer * factorial(integer - 1)) */ : 1;
```

The condition of the If Else statement is if integer == true. That must be (in our case) any positive number. And by now we know that if the condition is false it will return value of 1.

```/* const factorial = integer => integer ?*/ (integer * factorial(integer - 1)) /* : 1; */
```

Here comes the fun part. If the condition is evaluated as true, we will multiply the integer variable by the result of the factorial function itself with the integer value being deducted by 1 (integer - 1).

Now we see that factorial function is a loop function that calls itself as long as the value of integer(that is being deducted by 1 with every loop) is greater than 0.

```/* const factorial = integer =>*/ integer ? (integer * /*factorial*/(integer - 1))  : 1;
```

Once the statement evaluation of the integer ? becomes false (in our case 1 - 1 = 0) it will multiply the last assigned value of integer by 1 (: 1) and stops the loop.

Let's see an example with integer variable having a value of 3.

```integer = 3;
const factorial = integer =>
integer ? // 1st loop: 3 == true  | 2nd loop: 2 == true | 3rd loop: 1 == true | 4th loop: 0 == false
(integer * factorial(integer - 1)) // 1st loop: 3 * 2 | 2nd loop: 6 * 1
: 1; // 6 * 1 and stops the loop
factorial(integer); // 6
```

Hope you enjoyed this algorithm code breakdown and let me know if you have any other favourite solution (there's many off course).