DEV Community

Cover image for Top 7 best Algorithms to Improve your JavaScript Skills๐Ÿš€ ๐Ÿš€
Ashish donga
Ashish donga

Posted on

 

Top 7 best Algorithms to Improve your JavaScript Skills๐Ÿš€ ๐Ÿš€

Today I will teach you top 7 best algorithms to improve your JavaScript skills

best Algorithms to Improve JavaScript Skills as programmer

so let's start to understand JavaScript algorithms

1. how to Find the missing number from the array

const find_missing = function(input) {
  let n = input.length + 1;
let sum = 0;
  for (let i in input) {
    sum += input[i];
  }
return Math.floor((n * (n + 1)) / 2) - sum;
};
Enter fullscreen mode Exit fullscreen mode
Input: [1, 2, 3, 4, 6, 7, 8, 9, 10]
Output: 5

Enter fullscreen mode Exit fullscreen mode

2. Inverting integers in JavaScript

const reverse = function(num) {
    let result = 0;
    while (num !== 0) {
      result = result * 10 + num % 10;
      // Math.trunc() ๆ–นๆณ•ไผšๅฐ†ๆ•ฐๅญ—็š„ๅฐๆ•ฐ้ƒจๅˆ†ๅŽปๆމ๏ผŒๅชไฟ็•™ๆ•ดๆ•ฐ้ƒจๅˆ†
      num = Math.trunc(num / 10);
    }
if (result > 2**31 || result < -(2**31)) return 0;
    return result;
};

Enter fullscreen mode Exit fullscreen mode
Input: num = 123
Output: 321
Input: num = -123
Output: -321
Enter fullscreen mode Exit fullscreen mode

3.Array alignment

const permute = function(nums) {
    let results = [];
let go = (current) => {
      if (current.length === nums.length){
        results.push(current);
        return;
      }
      nums.forEach(n => {
        if (!current.includes(n)){
          go([...current, n]);
        }
      });
    }
    go([]);
    return results;
};
Enter fullscreen mode Exit fullscreen mode
Input: [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Enter fullscreen mode Exit fullscreen mode

4.Alignment in strings

const checkPermutation = function(s1, s2) {
  const len1 = s1.length, len2 = s2.length;
  if (len1 > len2) return false;
const count = Array(26).fill(0);
  for (let i = 0; i < len1; i++) {
      count[s1.charCodeAt(i)-97]++;
      count[s2.charCodeAt(i)-97]--;
  }
  if (!count.some(e => e !== 0)) return true;
for (let i = len1; i < len2; i++) {
      count[s2.charCodeAt(i)-97]--;
      count[s2.charCodeAt(i-len1)-97]++;
      if (!count.some(e => e !== 0)) return true;
  }
  return false;
};
Enter fullscreen mode Exit fullscreen mode
Input: s1 = โ€œabโ€, s2 = โ€œeidbaoโ€
Output: true
Input: s1 = โ€œaaโ€, s2 = โ€œeidbaoโ€
Output: false

Enter fullscreen mode Exit fullscreen mode

5.Longest valid brackets

const longestValidParentheses = function(S) {
  let stack = [-1], ans = 0;
  for (let i = 0; i < S.length; i++)
    if (S[i] === '(') stack.push(i)
    else if (stack.length === 1) stack[0] = i
    else stack.pop(), ans = Math.max(ans, i - stack[stack.length-1])
  return ans
};
Enter fullscreen mode Exit fullscreen mode
Input: โ€œ(()โ€
Output: 2
Input: โ€œ)()())โ€
Output: 4
Enter fullscreen mode Exit fullscreen mode

6.String multiplication

const multiply = function(num1, num2) {
 if (num1 == 0 || num2 == 0) return โ€˜0โ€™;
 const result = [];
for (let a = num1.length โ€” 1; a >= 0; a โ€” ) {
 for (let b = num2.length โ€” 1; b >= 0; b โ€” ) {
 const p1 = a + b;
 const p2 = a + b + 1;
 const sum = (result[p2] ?? 0) + num1[a] * num2[b];
result[p1] = (result[p1] ?? 0) + Math.floor(sum / 10);
 result[p2] = sum % 10;
 }
 }
 result[0] == 0 && result.shift();
 return result.join(โ€˜โ€™);
};
Enter fullscreen mode Exit fullscreen mode
Input: num1 = โ€œ2โ€, num2 = โ€œ3โ€
Output: โ€œ6โ€
Enter fullscreen mode Exit fullscreen mode

4Sum

const fourSum = function(nums, target) {
  let result = [];
  let length = nums.length;
  if (length < 4) return result; 
  nums = nums.sort((a, b) => a - b );
for (let i = 0; i < length - 3; i++) {
    if (nums[i] === nums[i - 1]) continue;
    for (let j = i + 1; j < length - 2; j++) {
      if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let k = j + 1;
      let l = length - 1;
while (k < l) {
        const sum = nums[i] + nums[j] + nums[k] + nums[l];
if (sum === target) {
          result.push([nums[i], nums[j], nums[k], nums[l]])
        }
if (sum <= target) {
          k += 1;
          while (nums[k] === nums[k - 1]) {
            k += 1;
          }
        }
if (sum >= target) {
          l -= 1;
          while (nums[l] === nums[l + 1]) {
            l -= 1;
          }
        }
      }
    }
  }
return result;
};
Enter fullscreen mode Exit fullscreen mode

thank you for reading our best Algorithms to Improve your JavaScript Skills guide

if you have any suggestion than comment

Top comments (1)

Collapse
 
imakashrana profile image
Akash Chauhan

The importance of gaming in Javascript is vast. It helps with hand-eye coordination, problem solving, and logic skills. Gaming can also relieve stress, improve moods, and promote social interaction.
Here find how you can improve your JavaScript skills with Javascript gaming

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but itโ€™s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!