Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common algorithm questions in JS interview

Shulou Source: shulou.com Published: 2022-06-01 09:35:17 09月30日 Update

This article mainly explains "what are the common algorithm questions in JS interview". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common algorithm questions in JS interview"?

1. Verify whether a number is a prime

1. If the number is 2 or 3, it must be prime

2. If it is an even number, it must not be a prime

3. If this number is not divisible by 3mure-any of its square roots, m must be a prime. And the divisor can be incremented each time (excluding even numbers)

Function isPrime (num) {

If (num = 2 | | num = 3) {

Return true

}

If (num% 2 = = 0) {

Return false

}

Let divisor = 3 limit = Math.sqrt (num)

While (limit > = divisor) {

If (num% divisor = = 0) {

Return false

}

Else {

Divisor + = 2

}

}

Return true

}

Console.log (isPrime (30)); / / false

two。 Fibonacci

The simplest thing to do: recursion.

Function fibonacci (n) {

If (n = divisor) {

If (a%divisor = 0 & & b%divisor = = 0) {

Res = divisor

}

Divisor++

}

Return res

}

Console.log (greatestCommonDivisor (8,4)); / / 4

Console.log (greatestCommonDivisor (69,169); / / 1

Solution 2:

Function greatestCommonDivisor (a _ r _ b) {

If (b = 0) {

Return a

} else {

Return greatestCommonDivisor (bmaine a% b)

}

}

4. Array deduplication

Traverse the original array

Get the value j of arr [I]

The value corresponding to the position j of the auxiliary array exits. If not, it is proved that the value of arr [I] is not duplicated.

At this point, the value j is stored in the res array, and the value of the auxiliary array j position is set to true.

Finally, the res array is returned.

Thank you for your reading, the above is the content of "what are the common algorithm questions in JS interview". After the study of this article, I believe you have a deeper understanding of what are the common algorithm questions in JS interview, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Array front end divisor learning common algorithm prime position even content problem recursion auxiliary verification complex large and so on continuous number practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Huawei Shulou Information NVidia