Assignment 1
Assignment 1
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
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"
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
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]
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"