Back to basics : Top interview logical programs in javascript

Cover Image for Back to basics : Top interview logical programs in javascript
Ullas Kishan
Ullas Kishan
5 min read
  • 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

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.

⬆ Back to Top

  • 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"

⬆ Back to Top

  • 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);

⬆ Back to Top

  • 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

⬆ Back to Top

Related tags

More Stories

A Guide to Resolving Git Conflicts in Your Local Setup
A Guide to Resolving Git Conflicts in Your Local Setup

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.

Ullas Kishan
2 min read
Unlocking the Secrets of Firebase Authentication with Google in Next.js
Unlocking the Secrets of Firebase Authentication with Google in Next.js

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.

Ullas Kishan
5 min read
[Solved] Firebase auth/invalid-api-key error when setting the values in the environment variable on NextJS?
[Solved] Firebase auth/invalid-api-key error when setting the values in the environment variable on NextJS?

Why am I getting an auth/invalid-api-key error when setting the Firebase values in the environment variable on NextJS ?

Ullas Kishan
2 min read
How to setup jest test case in NextJS
How to setup jest test case in NextJS

Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.

Ullas Kishan
4 min read
[Solved] How to download SVG as PNG file
[Solved] How to download SVG as PNG file

Easist way of downloading the SVG file as PNG file is done using javascript snippet

Ullas Kishan
2 min read
Some useful GIT commands for beginners
Some useful GIT commands for beginners

To keep the code is safe and distrubuted between multiple resources that been achieved with the help of GIT

Ullas Kishan
3 min read
How to set the http response headers in NextJS websites
How to set the http response headers in NextJS websites

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.

Ullas Kishan
3 min read
What are the different types of HTTP Security headers ?
What are the different types of HTTP Security headers ?

An HTTP header is a response by a web server to a browser that is trying to access a web page.

Ullas Kishan
4 min read
[Solved] How to create Application insight access token
[Solved] How to create Application insight access token

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.

Ullas Kishan
2 min read
How to do lazy loading in ReactJS
How to do lazy loading in ReactJS

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.

Ullas Kishan
3 min read
Javascript interview questions and answers
Javascript interview questions and answers

We covered most asked questions for Javascript interview and their answers

Ullas Kishan
8 min read
How to insert emojis to the html page
How to insert emojis to the html page

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.

Ullas Kishan
2 min read
What are ES6 features ?
What are ES6 features ?

ES6 or the ECMAScript 2015 is the major edition of ECMAScript language, it introduced several new features which are very special to the developers

Ullas Kishan
7 min read
Different versions of the ECMAscript
Different versions of the ECMAscript

what are the new features among the various versions of ECMA script and what is difference

Ullas Kishan
2 min read
[Solved] How to squash the number of commits from a branch in Git
[Solved] How to squash the number of commits from a branch in Git

We can squash the number of commits from a git branch

Ullas Kishan
3 min read
[Solved] Your focus-trap must have at least one container with at least one tabbable node in it at all times
[Solved] Your focus-trap must have at least one container with at least one tabbable node in it at all times

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

Ullas Kishan
2 min read
Writing test cases for modal popup in Jest
Writing test cases for modal popup in Jest

Writing test cases for modal popup in jest

Ullas Kishan
2 min read
[Solved] Uncaught TypeError: Cannot read property 'location' of undefined
[Solved] Uncaught TypeError: Cannot read property 'location' of undefined

Cannot read property location of undefined, this is an common test cases error in react jest while using useLocation hook in your react component

Ullas Kishan
2 min read
[Solved] Missing ID attributes in markdown to html
[Solved] Missing ID attributes in markdown to html

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

Ullas Kishan
1 min read
How to read the markdown files in ReactJS
How to read the markdown files in ReactJS

Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents.

Ullas Kishan
2 min read
Some useful techniques for array operation using javascript
Some useful techniques for array operation using javascript

There are few development tips for Javascript array operation, these tips will reduce your development time.

Ullas Kishan
3 min read
How to setup auto generated sitemap in nextjs
How to setup auto generated sitemap in nextjs

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.

Ullas Kishan
3 min read
How to know whether we are using mobile or desktop
How to know whether we are using mobile or desktop

This question is very usual, to get solve this issue by using the browser property user agent to check whether the device type.

Ullas Kishan
1 min read
What are the possible ways to create objects in JavaScript
What are the possible ways to create objects in JavaScript

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.

Ullas Kishan
2 min read