0% found this document useful (0 votes)
2 views

Assignment 1

The document outlines a series of programming assignments that require the creation of various functions in a programming language. These functions cover a range of tasks including arithmetic operations, string manipulation, array filtering, and object handling. Each assignment provides specific input and expected output for clarity.

Uploaded by

ea2878593
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 1

The document outlines a series of programming assignments that require the creation of various functions in a programming language. These functions cover a range of tasks including arithmetic operations, string manipulation, array filtering, and object handling. Each assignment provides specific input and expected output for clarity.

Uploaded by

ea2878593
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 1

1. Create a function that calculates the sum of two given numbers.


Input: 3, 5
Output: 8

2. Write a function that checks if a number is prime (a number that can only be divided
by 1 and itself without any remainder).
Input: 7
Output: true

3. Write a function to reverse a given string (using built in method).


Input: "hello"
Output: "olleh"

4. Write a function to find the largest number in an array.


Input: [1, 3, 7, 2, 4]
Output: 7

5. Write a function that filters an array and returns only the even numbers.
Input: [1, 2, 3, 4, 5, 6]
Output: [2, 4, 6]

6. Implement a function to reverse a string without using the built-in reverse() method.
Input: "route"
Output: "etuor"

7. Write a function to calculate the average value of all numbers in an array.


Input: [1, 2, 3, 4, 5]
Output: 3
8. Write a function that determines whether a given day number (1-7) represents a
weekday or weekend.
Input: 5
Output: "Weekday"
Input: 7
Output: "Weekend"

9. Write a function that filters an array of numbers and returns only those that are divisible
by 2 or 3.
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output: [2, 3, 4, 6, 8, 9]

10. Write a function that finds the index of a given element in an array. If the element isn't
found, return `-1`.
Input: [1, 2, 3, 4, 5], 3
Output: 2
Input: [1, 2, 3, 4, 5], 10
Output: -1

11. Write a function to calculate the factorial of a given number.


Input: 5
Output: 120

12. Write a function that takes an object and returns an array containing only its keys.
Input: {name: "John", age: 30}
Output: ["name", "age"]

13. Write a function that returns only the unique numbers from an array.
Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 3, 5]

14. Write a function to count the occurrences of each character in a string.


Input: "hello"
Output: {h: 1, e: 1, l: 2, o: 1}

15. Write a function that sorts an array of numbers in ascending order.


Input: [5, 3, 8, 1, 2]
Output: [1, 2, 3, 5, 8]
16. Write a function to check if a given string is an anagram of another string (i.e., contains
the same characters in a different order).
Input: "listen", "silent"
Output: true

17. Write a function that removes all falsy values (`false`, `null`, `0`, `""`, `undefined`,
and `NaN`) from an array.
Input: [0, false, "Hello", "", null, undefined, NaN, 42]
Output: ["Hello", 42]

18. Write a function that creates a car object with properties such as `model` and `year’
and includes a method to display the car's details.
Input: Toyota, 2020
Output: "Model: Toyota, Year: 2020"

19. Write a function that checks if a given object contains a specific property.
Input: {name: "Alice", age: 25}, "name"
Output: true
Input: {name: "Alice", age: 25}, "address"
Output: false

20. Write a function to count the number of vowels (a, e, i, o, u) in a string, regardless of
case.
Input: "Hello World"
Output: 3

21. Write a function that splits a string into an array of words based on spaces.
Input: "The quick brown fox"
Output: ["The", "quick", "brown", "fox"]

22. Write a function that performs a mathematical operation (`+`, `-`, `*`, `/`) on two
numbers.
Input: 5, 3, "+"
Output: 8
Input: 5, 3, "%"
Output: "Invalid operator"

You might also like