easy-intermediate programming questions taken from ChatGPT
-
Write a function in JavaScript that takes an array of numbers as input and returns a new array with all the even numbers from the input array.
-
Write a function in JavaScript that takes a string as input and returns the same string with all the vowels removed.
-
Write a function that takes two arrays of numbers as input and returns a new array that contains only the elements that are present in both arrays. For example, if the two input arrays are [1, 2, 3, 4] and [3, 4, 5, 6], the function should return [3, 4].
-
Write a function that takes an array of strings as input and returns the longest string in the array. If there are multiple strings with the same maximum length, return the first one. For example, if the input array is ["dog", "cat", "bird", "elephant"], the function should return "elephant".
-
Write a function that takes a string as input and returns the reverse of the string. For example, if the input string is "hello", the function should return "olleh".
-
Write a function that takes an array of numbers as input and returns the sum of all the positive numbers in the array. For example, if the input array is [-1, 3, 5, -2, 8], the function should return 16 (3 + 5 + 8).
-
Write a function that takes an array of numbers as input and returns a new array that contains only the numbers that are greater than zero and even. For example, if the input array is [1, 2, -3, 4, 0, 7, 8], the function should return [2, 4, 8].
-
Write a function that takes two parameters, a string and a letter, and returns the number of times the letter appears in the string (case-insensitive). For example, if the input string is "Hello, World" and the letter is "o", the function should return 2.
-
Write a function that takes an array of numbers as input and returns the second largest number in the array. For example, if the input array is [3, 5, 1, 4, 6, 2], the function should return 5.
-
Write a function that finds the largest value from a given array wihtout sorting the array.
-
Write a function called sumDigits that takes an integer as input and returns the sum of its digits. For example, sumDigits(123) should return 6 (since 1 + 2 + 3 = 6).
-
Write a function in JavaScript that takes a string as input and returns true if the string is a palindrome (i.e. if it reads the same backward as forward), and false otherwise. Ignore spaces, punctuation, and capitalization when determining whether the string is a palindrome. For example, "A man, a plan, a canal: Panama" is a palindrome.
-
Write a function in JavaScript that takes an array of integers as input and returns the largest difference between any two elements in the array. For example, if the input array is [1, 2, 4, 7], the function should return 6 (the difference between 1 and 7).
-
Write a function in JavaScript that takes a string as input and returns a new string that contains only the first occurrence of each character in the input string, in the order in which they appear. For example, if the input string is "banana", the function should return "ban".
-
Write a function in JavaScript that takes a string as input and returns the number of vowels in the string. For the purposes of this problem, consider the vowels to be "a", "e", "i", "o", and "u", regardless of case. For example, if the input string is "Hello, World!", the function should return 3.
-
Write a function in JavaScript that takes an array of objects as input, where each object represents a book with a title and an author. The function should return an array of authors, where each author appears only once in the array, in alphabetical order. For example, if the input array is [{title: "To Kill a Mockingbird", author: "Harper Lee"}, {title: "1984", author: "George Orwell"}, {title: "Pride and Prejudice", author: "Jane Austen"}, {title: "To Kill a Mockingbird", author: "Harper Lee"}], the function should return ["George Orwell", "Harper Lee", "Jane Austen"].
-
Write a program in JavaScript that prints out the numbers from 1 to 100, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both 3 and 5 with "FizzBuzz".
-
Write a program in JavaScript that finds the first non-repeating character in a string and returns it. For example, given the string "hello", the program should return "h". If there are no non-repeating characters, the program should return null.