Back to basics : Top interview logical programs in javascript
-
The most asked logical programs in the interview
During the technical interview, these types of logical programs may ask you. These questions are common in any software programmer role. I have picked a bunch of logical programming interview question that are using javascript and it may help you to crack your interview. The following programs to be narrated.
Table of contents
-
1. Fibonacci series using recursive function in javascript
In this program snippet you going to learn how to write the Fibonacci series. To understand the logic you should need javascript knowledge. Also we going to learn or familiar the recursive function. A recursive function is a function in code that refers to itself for execution. Recursive functions can be simple or elaborate. What is fibonacci? A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.
Let's go to the program and will examine how it works!
// program to display fibonacci sequence using recursion
function fibonacci(num) {
if(num < 2) {
return num;
}
else {
return fibonacci(num-1) + fibonacci(num - 2);
}
}
// set the nth term
let nThTerms = 5;
if(nThTerms <=0) {
console.log('Enter a positive integer.');
}
else {
for(let i = 0; i < nThTerms; i++) {
console.log(fibonacci(i));
}
}
Result
Enter the number of terms: 5
0
1
1
2
3
Hope the program is helped to understand the logic, we go to next coding example.
-
2. Find the prime numbers in javascirpt
let's do the coding for finding out the prime number. What is prime number A prime number is a number that is divisible by 1 and itself only. E.g.:- prime numbers are: 2, 3, 5, 7, 11, 13, 17... Basically, when you doing division to any number, if the remainder is 0 then it is a prime number otherwise it is not.
const primeNumber=number % i == 0
function isPrimeNumber(num) {
let isPrime = true;
if (num == 1) {
return true;
} else {
for (let i = 2; i < num; i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
console.log(isPrime + " is a prime number");
} else {
console.log(isPrime + " is not a prime number");
}
}
}
isPrimeNumber(10)
isPrimeNumber(11)
Result
10 is a prime number
11 is a prime number
if you want to check the series of prime number then use for loop and use the isPrimeNumber function and pass the number.
for (let i = 2; i < 10; i++)
{
isPrimeNumber(i)
}
Result
"2 is a prime number"
"3 is a prime number"
"4 is not a prime number"
"5 is a prime number"
"6 is not a prime number"
"7 is a prime number"
"8 is not a prime number"
"9 is not a prime number"
-
3. Pyramid star pattern in javascript**
The pyramid pattern is very popular star pattern, you can see the example below
*
***
*****
*******
*********
To achieve this pattern use the below code in your program.
let n = 5;
let string = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n - i; j++) {
string += " ";
}
for (let k = 0; k < 2 * i - 1; k++) {
string += "*";
}
string += "\n";
}
console.log(string);
-
4. Check amstrong number using javascript
In this program, you will learn to write a program for finding out the given number is amstrong or not. What is amstrong number if the sum of its own digits raised to the power number of digits gives the number itself.
function amstrong(num) {
let num1 = num;
let sum = 0;
while (num > 0) {
rem = num % 10;
sum = sum + Math.pow(rem, num1.toString().length);
num = parseInt(num / 10);
}
if (sum == num1) {
console.log(num+" is Armstrong");
} else {
console.log(num+" is not Armstrong");
}
}
amstrong(153);
amstrong(154);
Result
'153 is Armstrong'
'154 is not Armstrong'
You might have doubt how 153 is Armstrong and why 154 is not.
153 = (111)+(555)+(333)
where:
(111)=1
(555)=125
(333)=27
So:
1+125+27=153
More Stories
Cross-Origin Resource Sharing (CORS) is a security feature that lets a web page from one domain request resources from a different domain
SVG elements will not add the accessibility atttributes by default, so that will fail to describe by itself, and the NVDA and other screen reader required these attributes to work.
Despite being acquainted with git, many developers struggle to resolve these conflicts due to a lack of understanding of how to pull the conflict details into their local machines.
Firebase Authentication is one of its gems, allowing you to add user authentication effortlessly. It's secure, reliable, and comes with Google's seal of approval.
Why am I getting an auth/invalid-api-key error when setting the Firebase values in the environment variable on NextJS ?
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.
Easist way of downloading the SVG file as PNG file is done using javascript snippet
To keep the code is safe and distrubuted between multiple resources that been achieved with the help of GIT
The importance of the http response headers are highly needed to protect the websites from hackers. If you poorly managed the response header then one day the website will be compromise to the hacker.
An HTTP header is a response by a web server to a browser that is trying to access a web page.
Application Insights is an feature of Azure Monitor and it provides application performance monitoring features. APM tools are very useful to analyse applications from development, testing and production release.
A lazy function lets you defer the loading of a components code until it is rendered for the first time. Before, it will remain in the bundle. So that we can reduce the load of the application.
We covered most asked questions for Javascript interview and their answers
we are displaying these emojis with the help of ASCII code and it is not that easy to remember because its a mix of numeric and special characters.
ES6 or the ECMAScript 2015 is the major edition of ECMAScript language, it introduced several new features which are very special to the developers
what are the new features among the various versions of ECMA script and what is difference
We can squash the number of commits from a git branch
Your focus-trap must have at least one container with at least one tabbable node in it at all times, when using dialog or modal in ReactJS or other front-end framework
Writing test cases for modal popup in jest
Cannot read property location of undefined, this is an common test cases error in react jest while using useLocation hook in your react component
There is a common problem when parsing the markdown file the ID attribute is missing in the element, here we found a solution to fix/overcome
Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents.
There are few development tips for Javascript array operation, these tips will reduce your development time.
For every website the Sitemap will be playing important role for SEO performance. In Ecommerce and other consumer websites also SEO have important role for developing their revenue.
This question is very usual, to get solve this issue by using the browser property user agent to check whether the device type.
What are the possible ways to create objects in JavaScript, The traditional way to create an empty object is using the Object constructor. But currently this approach is not recommended.