Javascript AnswerDeck

JavaScript AnswerDeck


Developed by Alish Gosai

Questions and Solutions by: Alish Gosai & Alish Pawn


Question 1

Question 1

Description

Write a function greet that takes a name as a parameter and returns a greeting message. For example, if the input is "Alish", the function should return "Hello, Alish!".

Hint

Hint You can use string concatenation to combine "Hello, " with the input name and "!".

Expected Output

Output The function greet("Alish") should return "Hello, Alish!".

Solution

View Solution


Question 2

Question 2

Description

Create a function sum that takes two numbers as parameters and returns their sum. For example, if the inputs are 5 and 3, the function should return 8.

Hint

Hint You can simply add the two parameters together using the + operator.

Expected Output

Output The function sum(5, 3) should return 8.

Solution

View Solution


Question 3

Question 3

Description

Write a function isEven that checks if a given number is even. The function should return true if the number is even, and false otherwise.

Hint

Hint You can use the modulo operator % to check if a number is divisible by 2.

Expected Output

Output The function isEven(4) should return true, and isEven(7) should return false.

Solution

View Solution


Question 4

Question 4

Description

Create a function reverseString that takes a string as input and returns the string reversed. For example, if the input is "hello", the function should return "olleh".

Hint

Hint You can use the split, reverse, and join methods on strings.

Expected Output

Output The function reverseString("hello") should return "olleh".

Solution

View Solution


Question 5

Question 5

Description

Write a function countVowels that counts the number of vowels in a given string. Vowels are a, e, i, o, and u. For example, the string "hello" contains 2 vowels.

Hint

Hint You can loop through the string and check if each character is a vowel.

Expected Output

Output The function countVowels("hello") should return 2.

Solution

View Solution


Question 6

Question 6

Description

Create a function multiplyArray that takes an array of numbers and returns the product of all the numbers. For example, for the array [2, 3, 4], the function should return 24.

Hint

Hint You can use a loop to multiply all the elements of the array together.

Expected Output

Output The function multiplyArray([2, 3, 4]) should return 24.

Solution

View Solution


Question 7

Question 7

Description

Write a function findMax that takes an array of numbers and returns the largest number in the array. For example, for the array [1, 3, 5, 2], the function should return 5.

Hint

Hint You can use a loop to compare each number and keep track of the maximum.

Expected Output

Output The function findMax([1, 3, 5, 2]) should return 5.

Solution

View Solution


Question 8

Question 8

Description

Create a function isPalindrome that checks if a given string is a palindrome. A palindrome is a string that reads the same backward as forward. For example, "madam" is a palindrome.

Hint

Hint You can compare the string with its reversed version.

Expected Output

Output The function isPalindrome("madam") should return true, and isPalindrome("hello") should return false.

Solution

View Solution


Question 9

Question 9

Description

Write a function filterEvens that takes an array of numbers and returns a new array containing only the even numbers.

Hint

Hint You can use the filter method to create a new array with only even numbers.

Expected Output

Output The function filterEvens([1, 2, 3, 4, 5]) should return [2, 4].

Solution

View Solution


Question 10

Question 10

Description

Create a function removeDuplicates that takes an array of numbers and returns a new array with duplicates removed. For example, the array [1, 2, 2, 3] should become [1, 2, 3].

Hint

Hint You can use a Set to remove duplicates from the array.

Expected Output

Output The function removeDuplicates([1, 2, 2, 3]) should return [1, 2, 3].

Solution

View Solution


Question 11

Question 11

Description

You are building a library management system where you need to keep track of the books available for lending. Write a function getAvailableBooks that takes an array of book objects, each with a title and isAvailable property, and returns an array of titles of books that are currently available.

Hint

Hint Filter the array of books based on the isAvailable property.

Expected Output

Output The function getAvailableBooks([{ title: 'Book 1', isAvailable: true }, { title: 'Book 2', isAvailable: false }]) should return ['Book 1'].

Solution

View Solution


Question 12

Question 12

Description

You are developing a chat application and need to implement a feature that formats messages with timestamps. Write a function that takes a message and a timestamp and returns the formatted message in the form “message (HH:MM:SS)”. This will help in displaying messages with the time they were sent, making the chat more organized and user-friendly.

Hint

Hint Use the Date object to format the timestamp.

Expected Output

Output The function formatMessage('Hello', 1628257325000) should return 'Hello (12:02:05)'.

Solution

View Solution


Question 13

Question 13

Description

You are creating a survey application where users can rate their satisfaction on a scale of 1 to 5. Write a function that takes an array of ratings and returns the average rating. This will help in understanding the overall user satisfaction from the survey results.

Hint

Hint Sum the ratings and divide by the number of ratings to get the average.

Expected Output

Output The function calculateAverageRating([5, 4, 3, 5, 4]) should return 4.2.

Solution

View Solution


Question 14

Question 14

Description

You are building a weather application and need to convert temperatures from Fahrenheit to Celsius. Write a function that takes a temperature in Fahrenheit and returns the equivalent temperature in Celsius. This will allow users to view temperature data in their preferred unit.

Hint

Hint Use the conversion formula C = (F - 32) * 5/9 to calculate Celsius.

Expected Output

Output The function convertToCelsius(68) should return 20.

Solution

View Solution


Question 15

Question 15

Description

You are developing a financial application that tracks daily expenses. Write a function that takes an array of expense objects, each with a date and amount property, and returns the total amount spent. This will help users manage their finances by providing a summary of their daily spending.

Hint

Hint Sum the amount property of each expense object to get the total.

Expected Output

Output The function calculateTotalExpenses([{ date: '2023-01-01', amount: 50 }, { date: '2023-01-02', amount: 30 }]) should return 80.

Output Describe the expected result or output of Question 15. Specify what the function or solution should return or how it should behave.

Solution

View Solution


Question 16

Question 16

Description

You are creating a contact list application where users can search for contacts by name. Write a function that takes an array of contact objects, each with a name and phoneNumber property, and a search term. The function should return an array of contacts whose names include the search term. This will allow users to quickly find and retrieve contact information.

Hint

Hint Use the includes method to check if the contact’s name contains the search term.

Expected Output

Output The function searchContacts([{ name: 'Alice', phoneNumber: '1234567890' }, { name: 'Bob', phoneNumber: '0987654321' }], 'Ali') should return [{ name: 'Alice', phoneNumber: '1234567890' }].

Solution

View Solution


Question 17

Question 17

Description

You are working on an e-commerce platform and need to apply a discount to a list of product prices. Write a function that takes an array of product prices and a discount percentage, and returns an array of the discounted prices. This will help in displaying the final prices after applying discounts during sales or promotions.

Hint

Hint Apply the discount percentage to each price to calculate the discounted price.

Expected Output

Output The function applyDiscount([100, 200, 300], 10) should return [90, 180, 270].

Solution

View Solution


Question 18

Question 18

Description

You are developing a simple game where the player needs to guess a random number. Write a function that generates a random integer between a given minimum and maximum value (inclusive). This will create an element of chance in the game, making it more engaging for the player.

Hint

Hint Use Math.random() to generate a random number and scale it to the desired range.

Expected Output

Output The function generateRandomNumber(1, 10) should return a random integer between 1 and 10.

Solution

View Solution


Question 19

Question 19

Description

You are building a reminder application that sends notifications at specified times. Write a function that takes an array of reminder times (in milliseconds) and a current time, and returns an array of reminders that are due (i.e., times less than or equal to the current time). This will help in notifying users of tasks that need immediate attention.

Hint

Hint Filter the array of reminders based on the current time.

Expected Output

Output The function getDueReminders([1628257325000, 1628260925000], 1628257325000) should return [1628257325000].

Solution

View Solution


Question 20

Question 20

Description

You are creating a student attendance system. Write a function that takes an array of student objects, each with a name and attendance property, and returns the names of students who have perfect attendance (i.e., attendance is 100%). This will help in identifying and rewarding students who have maintained perfect attendance.

Hint

Hint Filter the array of students based on the attendance property.

Expected Output

Output The function getPerfectAttendance([{ name: 'Alice', attendance: 100 }, { name: 'Bob', attendance: 90 }]) should return ['Alice'].

Solution

View Solution


Question 21

Question 21

Description

You are developing a user profile page where users can update their profile information. Write a function that validates the user’s input for an email address and a phone number. The email should follow a standard format, and the phone number should contain exactly 10 digits. Log messages indicating whether each input is valid.

Hint

Hint Use regular expressions to validate the email address and check the length of the phone number.

Expected Output

Output The function should log whether the email address and phone number are valid.

Solution

View Solution


Question 22

Question 22

Description

You are developing a user profile page where users can update their profile information. Write a function that validates the user’s input for an email address and a phone number. The email should follow a standard format, and the phone number should contain exactly 10 digits. Log messages indicating whether each input is valid.

Hint

Hint Use regular expressions to validate the email address and check the length of the phone number.

Expected Output

Output The function should log whether the email address and phone number are valid.

Solution

View Solution


Question 23

Question 23

Description

You are working on a file management system. Write a function that takes an array of file sizes (in megabytes) and returns an array of file sizes that exceed a specified size limit.

Hint

Hint Use array methods to filter the file sizes based on the size limit.

Expected Output

Output The function should return an array of file sizes that exceed the size limit.

Solution

View Solution


Question 24

Question 24

Description

You are developing a budgeting tool. Write a function that calculates the total expenses and savings based on an array of expense and income amounts. The function should return an object with the total expenses, total income, and net savings (income minus expenses).

Hint

Hint Sum the expenses and income arrays and compute the net savings.

Expected Output

Output The function should return an object with total expenses, total income, and net savings.

Solution

View Solution


Question 25

Question 25

Description

You are creating a basic library system. Write a function that takes an array of book objects, each with a title, author, and publication year, and returns the books published after a specified year.

Hint

Hint Use array methods to filter books based on the publication year.

Expected Output

Output The function should return an array of books published after the specified year.

Solution

View Solution


Question 26

Question 26

Description

You are developing a simple weather app. Write a function that converts a temperature from Fahrenheit to Celsius and returns the result. The function should take a temperature in Fahrenheit as input and use the formula C = (F - 32) * 5/9 for the conversion.

Hint

Hint Apply the formula for Fahrenheit to Celsius conversion.

Expected Output

Output The function should return the temperature in Celsius.

Solution

View Solution


Question 27

Question 27

Description

You are working on a content management system. Write a script that takes an array of article titles and checks if any title contains the keyword JavaScript. Log a message indicating which titles contain the keyword.

Hint

Hint Use array methods to check each title for the JavaScript keyword.

Expected Output

Output The script should log which article titles contain the JavaScript keyword.

Solution

View Solution


Question 28

Question 28

Description

You are building a simple financial report generator. Write a function that calculates the total and average amount of transactions from an array of transaction amounts. The function should return an object with the total and average values.

Hint

Hint Sum the transactions and calculate the average using array methods.

Expected Output

Output The function should return an object with total and average transaction amounts.

Solution

View Solution


Question 29

Question 29

Description

You are creating a contact list application. Write a function that takes an array of contact objects with name and phone number properties and returns an array of contact names whose phone number starts with a specific area code.

Hint

Hint Use array methods to filter contacts based on the phone number prefix.

Expected Output

Output The function should return an array of contact names with the specified area code.

Solution

View Solution


Question 30

Question 30

Description

You are developing a task management system. Write a function that takes an array of task objects, each with a due date and a status (completed or not), and returns an array of tasks that are overdue (i.e., have a due date before today and are not completed).

Hint

Hint Use date comparisons and array methods to filter overdue tasks.

Expected Output

Output The function should return an array of overdue tasks.

Solution

View Solution