Practice 50 JavaScript Functions Programming Questions, TechnoVlogs

Practice 50 JavaScript Functions Programming Questions


Q1. Write a JavaScript program to create a function that returns the sum of two numbers.
Input: addNumbers(5, 7)  
Expected Output: 12

Q2. Write a JavaScript program to create a function that returns the square of a number.  
Input: squareNumber(4)  
Expected Output: 16

Q3. Write a JavaScript program to create a function that concatenates two strings.  
Input: concatStrings("Hello", "World")  
Expected Output: "HelloWorld"

Q4. Write a JavaScript program to create a function that checks if a number is even.  
Input: isEven(10)  
Expected Output: true  

Input: isEven(7)  
Expected Output: false

Q5. Write a JavaScript program to create a function that reverses a string.  
Input: reverseString("apple")  
Expected Output: "elppa"

Q6. Write a JavaScript program to create a function that returns the factorial of a number.  
Input: factorial(5)  
Expected Output: 120

Q7. Write a JavaScript program to create a function that finds the maximum of two numbers.  
Input: findMax(10, 20)  
Expected Output: 20

Q8. Write a JavaScript program to create a function that calculates the area of a rectangle.  
Input: rectangleArea(5, 7)  
Expected Output: 35

Q9. Write a JavaScript program to create a function that converts Celsius to Fahrenheit.  
Input: celsiusToFahrenheit(0)  
Expected Output: 32

Q10. Write a JavaScript program to create a function that finds the length of a string.  
Input: stringLength("JavaScript")  
Expected Output: 10

Q11. Write a JavaScript program to create a function that returns the first character of a string.  
Input: firstCharacter("hello")  
Expected Output: "h"

Q12. Write a JavaScript program to create a function that returns the sum of an array of numbers.  
Input: sumArray([1, 2, 3, 4])  
Expected Output: 10

Q13. Write a JavaScript program to create a function that finds the largest number in an array.  
Input: largestNumber([3, 7, 2, 9])  
Expected Output: 9

Q14. Write a JavaScript program to create a function that converts a string to uppercase.  
Input: toUpperCase("javascript")  
Expected Output: "JAVASCRIPT"

Q15. Write a JavaScript program to create a function that counts the number of vowels in a string.  
Input: countVowels("hello")  
Expected Output: 2

Q16. Write a JavaScript program to create a function that checks if a number is positive.  
Input: isPositive(5)  
Expected Output: true  

Input: isPositive(-3)  
Expected Output: false

Q17. Write a JavaScript program to create a function that returns the last character of a string.  
Input: lastCharacter("world")  
Expected Output: "d"

Q18. Write a JavaScript program to create a function that calculates the sum of digits in a number.  
Input: sumDigits(123)  
Expected Output: 6

Q19. Write a JavaScript program to create a function that checks if a string is a palindrome.  
Input: isPalindrome("racecar")  
Expected Output: true  

Input: isPalindrome("hello")  
Expected Output: false

Q20. Write a JavaScript program to create a function that returns the absolute value of a number.  
Input: absoluteValue(-7)  
Expected Output: 7

Q21. Write a JavaScript program to create a function that multiplies all numbers in an array.  
Input: multiplyArray([1, 2, 3, 4])  
Expected Output: 24

Q22. Write a JavaScript program to create a function that finds the smallest number in an array.  
Input: smallestNumber([5, 3, 9, 1])  
Expected Output: 1

Q23. Write a JavaScript program to create a function that capitalizes the first letter of a string.  
Input: capitalizeFirst("javascript")  
Expected Output: "Javascript"

Q24. Write a JavaScript program to create a function that returns the index of an element in an array.  
Input: findIndex([10, 20, 30], 20)  
Expected Output: 1

Q25. Write a JavaScript program to create a function that removes spaces from a string.  
Input: removeSpaces("Hello World")  
Expected Output: "HelloWorld"

Q26. Write a JavaScript program to create a function that checks if a string contains another string.  
Input: containsString("hello world", "world")  
Expected Output: true  

Q27. Write a JavaScript program to create a function that finds the average of numbers in an array.  
Input: averageArray([10, 20, 30, 40])  
Expected Output: 25

Q28. Write a JavaScript program to create a function that checks if a number is prime.  
Input: isPrime(7)  
Expected Output: true  

Input: isPrime(10)  
Expected Output: false

Q29. Write a JavaScript program to create a function that calculates the perimeter of a rectangle.  
Input: rectanglePerimeter(5, 7)  
Expected Output: 24

Q30. Write a JavaScript program to create a function that converts an array of strings to uppercase.  
Input: toUpperCaseArray(["apple", "banana"])  
Expected Output: ["APPLE", "BANANA"]

Q31. Write a JavaScript program to create a function that returns the reverse of an array.  
Input: reverseArray([1, 2, 3, 4])  
Expected Output: [4, 3, 2, 1]

Q32. Write a JavaScript program to create a function that removes duplicates from an array.  
Input: removeDuplicates([1, 2, 2, 3, 3, 4])  
Expected Output: [1, 2, 3, 4]

Q33. Write a JavaScript program to create a function that calculates the power of a number.  
Input: power(2, 3)  
Expected Output: 8

Q34. Write a JavaScript program to create a function that returns the number of words in a string.  
Input: countWords("hello world")  
Expected Output: 2

Q35. Write a JavaScript program to create a function that generates a random number between two values.  
Input: randomBetween(1, 10)  
Expected Output: A number between 1 and 10 (inclusive).

Q36. Write a JavaScript program to create a function that checks if a string starts with a given substring.  
Input: startsWith("hello world", "hello")  
Expected Output: true

Q37. Write a JavaScript program to create a function that finds the median of an array of numbers.  
Input: findMedian([1, 3, 3, 6, 7, 8, 9])  
Expected Output: 6

Q38. Write a JavaScript program to create a function that converts a number to a string.  
Input: numberToString(123)  
Expected Output: "123"

Q39. Write a JavaScript program to create a function that checks if an array is sorted in ascending order.  
Input: isSorted([1, 2, 3, 4])  
Expected Output: true  

Input: isSorted([3, 2, 1])  
Expected Output: false

Q40. Write a JavaScript program to create a function that returns the nth Fibonacci number.  
Input: fibonacci(6)  
Expected Output: 8

Q41. Write a JavaScript program to create a function that removes all vowels from a string.  
Input: removeVowels("hello world")  
Expected Output: "hll wrld"

Q42. Write a JavaScript program to create a function that checks if a number is divisible by another number.  
Input: isDivisible(10, 2)  
Expected Output: true  

Input: isDivisible(10, 3)  
Expected Output: false

Q43. Write a JavaScript program to create a function that finds the GCD (greatest common divisor) of two numbers.  
Input: findGCD(12, 15)  
Expected Output: 3

Q44. Write a JavaScript program to create a function that converts a string to an array of characters.  
Input: stringToArray("hello")  
Expected Output: ["h", "e", "l", "l", "o"]

Q45. Write a JavaScript program to create a function that sorts an array in ascending order.  
Input: sortArray([3, 1, 4, 2])  
Expected Output: [1, 2, 3, 4]

Q46. Write a JavaScript program to create a function that repeats a string a given number of times.  
Input: repeatString("abc", 3)  
Expected Output: "abcabcabc"

Q47. Write a JavaScript program to create a function that checks if a string ends with a given substring.  
Input: endsWith("hello world", "world")  
Expected Output: true

Q48. Write a JavaScript program to create a function that finds the sum of all even numbers in an array.  
Input: sumEvenNumbers([1, 2, 3, 4, 5])  
Expected Output: 6

Q49. Write a JavaScript program to create a function that finds the index of the first occurrence of a value in an array.  
Input: findFirstIndex([1, 2, 3, 4, 2], 2)  
Expected Output: 1

Q50. Write a JavaScript program to create a function that converts a string to lowercase.  
Input: toLowerCase("JAVASCRIPT")  
Expected Output: "javascript"

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!