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).
1. Return the Sum of Two Numbers
Create a function that takes two numbers as arguments and return
their sum.
Examples:
addition(3, 2) ➞ 5addition(-3, -6) ➞ -9addition(7, 3) ➞ 10
You can find the answer to this question here.
2. Convert Minutes into Seconds
Write a function that takes an integer minutes
and converts it to seconds.
Examples
convert(10) ➞ 600convert(3) ➞ 180convert(2) ➞ 120
You can find the answer to this question here.
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= 50lessThan100(90, 15) ➞ false
// 90+ 15= 105lessThan100(30, 27) ➞ true
You can find the answer to this question here.
4. Are the Numbers Equal?
Create a function that returns true
when num1
is equal to num2
; otherwise, return false
.
Examples
isSameNum(4, 2) ➞ falseisSameNum(7, 7) ➞ trueisSameNum(4, "4") ➞ false
You can find the answer to this question here.
5. Is the String Empty?
Create a function that returns true
if a string is empty and false
otherwise.
Examples
isEmpty("") ➞ trueisEmpty("z") ➞ falseisEmpty(" ") ➞ false
You can find the answer to this question here.
6. Return a String as an Integer
Create a function that takes a string and returns it as an integer.
Examples
stringInt("7") ➞ 7stringInt("10") ➞ 10stringInt("987") ➞ 987
You can find the answer to this question here.
7. Solve the Equation
Create a function that takes an equation (e.g. "1+1"
), and returns the answer.
Examples
equation("3+1") ➞ 4equation("4*4-1") ➞ 15equation("1+2+3") ➞ 6
You can find the answer to this question here.
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([]) ➞ []
You can find the answer to this question here.
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]) ➞ 4getLastItem(["horse", "elephant", "dog"]) ➞ "dog"getLastItem([true, false ]) ➞ false
You can find the answer to this question here.
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") ➞ 2findIndex(["Red", "blue", "Blue", "Green"], "blue") ➞ 1findIndex(["a", "g", "y", "d"], "d") ➞ 3findIndex(["Pineapple", "Orange", "Grape", "Apple"], "Pineapple") ➞ 0
You can find the answer to this question here.
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) ➞ truecheck([1, 1, 2, 1, 1], 3) ➞ falsecheck([5, 5, 5, 6], 5) ➞ truecheck([], 5) ➞ false
You can find the answer to this question here.
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"
You can find the answer to this question here.
13. Return the Total Number of Parameters
Create a function that returns the total number of parameters passed in.
Examples
numberArgs("a", "b", "c") ➞ 3numberArgs(10, 20, 30, 40, 50) ➞ 5numberArgs(x, y) ➞ 2numberArgs() ➞ 0
You can find the answer to this question here.
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]
You can find the answer to this question here.
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"
You can find the answer to this question here.
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]) ➞ 10getSumOfItems([45, 3, 10]) ➞ 58getSumOfItems([-2, 84, 20]) ➞ 102
You can find the answer to this question here.
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?????"
You can find the answer to this question here.
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]
You can find the answer to this question here.
19. Check String for Spaces
Create a function that returns true
if a string contains any spaces.
Examples
hasSpaces("hello") ➞ falsehasSpaces("hello world") ➞ truehasSpaces(" ") ➞ truehasSpaces("") ➞ falsehasSpaces(",./!@#") ➞ false
You can find the answer to this question here.
20. Case Insensitive Comparison
Write a function that validates whether two strings are identical. Make it case insensitive.
Examples
match("hello", "hELLo") ➞ truematch("motive", "emotive") ➞ falsematch("venom", "VENOM") ➞ truematch("mask", "mAskinG") ➞ false
You can find the answer to this question here.
21. Multiply Every Array Item by Two
Create a function that takes a string and returns it as an integer.
Examples
stringInt("6") ➞ 6stringInt("1000") ➞ 1000stringInt("12") ➞ 12
You can find the answer to this question here.
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"]) ➞ []
You can find the answer to this question here.
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 = 9addOddToN(13) ➞ 49addOddToN(47) ➞ 576
You can find the answer to this question here.
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) ➞ 3findDigitAmount(56) ➞ 2findDigitAmount(7154) ➞ 4findDigitAmount(61217311514) ➞ 11findDigitAmount(0) ➞ 1
You can find the answer to this question here.
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"
You can find the answer to this question here.
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