Javascript interview logical questions -Very Easy

In recent years, the javascript is more advanced and there are lots of opportunities for javascript developers in the market, but cracking the Javascript interview is sometimes a bit tricky. So you need to prepare yourself from the basic building blocks to advanced javascript features. I have prepared a list of some javascript logical questions which you might get asked in your javascript interview. Try to solve these questions by yourself in a minimum line of code, I have also added the answers for each question on my Github repository(You will find an answer link after each question).

Javascript interview logical questions -Very Easy

1. Return the Sum of Two Numbers

Create a function that takes two numbers as arguments and return their sum.

Examples:

addition(3, 2) ➞ 5
addition(-3, -6) ➞ -9
addition(7, 3) ➞ 10

Answer

function addition(a, b) {
return a + b;
}

2. Convert Minutes into Seconds

Write a function that takes an integer minutes and converts it to seconds.

Examples:

convert(10) ➞ 600
convert(3) ➞ 180
convert(2) ➞ 120

Answer

function convert(minutes) {
return minutes * 60
}

3. Less Than 100?

Given two numbers, return true if the sum of both numbers is less than 100. Otherwise, return false.

Examples:

lessThan100(25, 25) ➞ true
// 25+ 25= 50
lessThan100(90, 15) ➞ false
// 90+ 15= 105
lessThan100(30, 27) ➞ true

Answer

function lessThan100(a, b) {
return a + b >= 100 ? false : true
}

4. Are the Numbers Equal?

Create a function that returns true when num1 is equal to num2; otherwise, return false.

Examples:

isSameNum(4, 2) ➞ false
isSameNum(7, 7) ➞ true
isSameNum(4, "4") ➞ false

Answer

function isSameNum(num1, num2) {
return num1 === num2 ? true : false
}

6. Return a String as an Integer

Create a function that takes a string and returns it as an integer.

Examples:

stringInt("7") ➞ 7
stringInt("10") ➞ 10
stringInt("987") ➞ 987

Answer

function stringInt(str) {
return parseInt(str)
}

7. Solve the Equation

Create a function that takes an equation (e.g. "1+1"), and returns the answer.

Examples:

equation("3+1") ➞ 4
equation("4*4-1") ➞ 15
equation("1+2+3") ➞ 6

Answer

function equation(s) {
return eval(s)
}

8. Reverse an Array

Write a function to reverse an array.

Examples:

reverse([1, 2, 3, 4, 5]) ➞ [5, 4, 3, 2, 1]
reverse([9, 2, 1, 5]) ➞ [5, 1, 2, 9]
reverse([]) ➞ []

Answer

function reverse(arr) {
return arr.reverse()
}

9. Return the Last Element in an Array

Create a function that accepts an array and returns the last item in the array.

Examples:

getLastItem([1, 2, 3, 4]) ➞ 4
getLastItem(["horse", "elephant", "dog"]) ➞ "dog"
getLastItem([true, false ]) ➞ false

Answer

function getLastItem(arr) {
return arr[arr.length - 1]
}

10. Find the Index

Create a function that takes an array and a string as arguments and return the index of the string.

Examples:

findIndex(["hi", "edabit", "fgh", "abc"], "fgh") ➞ 2
findIndex(["Red", "blue", "Blue", "Green"], "blue") ➞ 1
findIndex(["a", "g", "y", "d"], "d") ➞ 3
findIndex(["Pineapple", "Orange", "Grape", "Apple"], "Pineapple") ➞ 0

Answer

function findIndex(arr, str) {
return arr.indexOf(str)
}

11. Check if an Array Contains a Given Number

Write a function to check if an array contains a particular number.

Examples:

check([1, 2, 3, 4, 5], 4) ➞ true
check([1, 1, 2, 1, 1], 3) ➞ false
check([5, 5, 5, 6], 5) ➞ true
check([], 5) ➞ false

Answer

function check(arr, el) {
return arr.includes(el)
}

12. Concatenating First and Last Character of a String

Creates a function that takes a string and returns the concatenated first and last character.

Examples:

firstLast("ganesh") ➞ "gh"
firstLast("kali") ➞ "ki"
firstLast("shiva") ➞ "sa"
firstLast("vishnu") ➞ "vu"
firstLast("durga") ➞ "da"

Answer

function firstLast(name) {
return name.slice(0, 1) + name.slice(name.length - 1, name.length)
}

13. Return the Total Number of Parameters

Create a function that returns the total number of parameters passed in.

Examples:

numberArgs("a", "b", "c") ➞ 3
numberArgs(10, 20, 30, 40, 50) ➞ 5
numberArgs(x, y) ➞ 2
numberArgs() ➞ 0

Answer

function numberArgs() {
return arguments.length
}

14. Multiply Every Array Item by Two

Create a function that takes an array with numbers and return an array with the elements multiplied by two.

Examples:

getMultipliedArr([2, 5, 3, 4]) ➞ [4, 10, 6, 8]
getMultipliedArr([1, 86, -5, 5]) ➞ [2, 172, -10, 10]
getMultipliedArr([5, 382, 0]) ➞ [10, 764, 0]

Answer

function getMultipliedArr(arr) {
return arr.map((e) => e * 2)
}

15. Spaces Between Each Character

Create a function that takes a string and returns a string with spaces in between all of the characters.

Examples:

spaceMeOut("space") ➞ "s p a c e"
spaceMeOut("far out") ➞ "f a r o u t"
spaceMeOut("elongated musk") ➞ "e l o n g a t e d m u s k"

Answer

function spaceMeOut(str) {
return str.split('').join(' ')
}

16. Get the Sum of All Array Elements

Create a function that takes an array and returns the sum of all numbers in the array.

Examples:

getSumOfItems([2, 5, 3]) ➞ 10
getSumOfItems([45, 3, 10]) ➞ 58
getSumOfItems([-2, 84, 20]) ➞ 102

Answer

function getSumOfItems(arr) {
var sum = 0;
arr.map((e) => sum += e );
return sum;
}

17. Modifying the Last Character

Create a function which makes the last character of a string repeat n number of times.

Examples:

modifyLast("Hello", 3) ➞ "Hellooo"
modifyLast("hey", 6) ➞ "heyyyyyy"
modifyLast("excuse me what?", 5) ➞ "excuse me what?????"

Answer

function modifyLast(str, n) {
return str + str.slice(-1).repeat(n - 1)
}

18. Return the First and Last Elements in an Array

Create a function that takes an array of numbers and return the first and last elements as a new array.

Examples:

firstLast([5, 10, 15, 20, 25]) ➞ [5, 25]
firstLast(["edabit", 13, null, false, true]) ➞ ["edabit", true]
firstLast([undefined, 4, "6", "hello", null]) ➞ [undefined, null]

Answer

function firstLast(arr) {
return [arr[0], arr[arr.length - 1]]
}

19. Check String for Spaces

Create a function that returns true if a string contains any spaces.

Examples:

hasSpaces("hello") ➞ false
hasSpaces("hello world") ➞ true
hasSpaces(" ") ➞ true
hasSpaces("") ➞ false
hasSpaces(",./!@#") ➞ false

Answer

function hasSpaces(str) {
return str.includes(' ') ? true : false
}

20. Case Insensitive Comparison

Write a function that validates whether two strings are identical. Make it case insensitive.

Examples:

match("hello", "hELLo") ➞ true
match("motive", "emotive") ➞ false
match("venom", "VENOM") ➞ true
match("mask", "mAskinG") ➞ false

Answer

function match(s1, s2) {
return s1.toLowerCase() == s2.toLowerCase() ? true : false
}

21. String to integer

Create a function that takes a string and returns it as an integer.

Examples:

stringInt("6") ➞ 6
stringInt("1000") ➞ 1000
stringInt("12") ➞ 12

Answer

function stringInt(item) {
return parseInt(item)
}

22. Filter Strings from Array

Create a function that takes an array of strings and numbers, and filters out the array so that it returns an array of integers only.

Examples:

filterArray([1, 2, 3, "a", "b", 4]) ➞ [1, 2, 3, 4]
filterArray(["A", 0, "Edabit", 1729, "Python", "1729"]) ➞ [0, 1729]
filterArray(["Nothing", "here"]) ➞ []

Answer

function filterArray(arr) {
return arr.filter(e => Number.isInteger(e))
}

23. Sum of the Odd Numbers

Create a function which returns the total of all odd numbers up to and including n. n will be given as an odd number.

Examples:

addOddToN(5) ➞ 9
// 1 + 3 + 5 = 9
addOddToN(13) ➞ 49
addOddToN(47) ➞ 576

Answer

function addOddToN(n) {
num = 0
for(i=1;i<=n;i++)
num += i%2 == 0 ? 0 : i
return num
}

24. Find the Total Number of Digits the Given Number Has

Create a function that takes a number as an argument and returns the amount of digits it has.

Examples:

findDigitAmount(123) ➞ 3
findDigitAmount(56) ➞ 2
findDigitAmount(7154) ➞ 4
findDigitAmount(61217311514) ➞ 11
findDigitAmount(0) ➞ 1

Answer

function findDigitAmount(num) {
return String(num).length
}

25. Convert Number to Corresponding Month Name

Create a function that takes a number (from 1 to 12) and returns its corresponding month name as a string. For example, if you’re given 3 as input, your function should return "March", because March is the 3rd month.

Examples:

monthName(3) ➞ "March"
monthName(12) ➞ "December"
monthName(6) ➞ "June"

Answer

function monthName(num) {
let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return months[num - 1]
}

I have faced these questions while interviewing in many companies, Some of the questions are referenced from internet sites like https://www.toptal.com/, https://www.thatjsdude.com/, https://www.fullstack.cafe/, https://edabit.com/.


Heads Up: I am working on one of the cool Chrome extensions which helps to format your code easily & it's superfast It can format your code within milliseconds. on top of it, it’s completely free for use. So go & hit that Add to Chrome Button. https://chrome.google.com/webstore/detail/code-formatter/njpgcnaadikbannefjibknjopmogeidm