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

js Task

The document outlines various programming tasks involving conditional statements and loops, including functions for grade evaluation, day classification, arithmetic operations, month days, traffic light actions, and age groups. It also includes tasks for printing numbers, calculating sums, finding prime numbers, and manipulating arrays such as finding sums, reversing, and merging. Additionally, it covers array methods for adding, removing, and extracting elements, as well as string manipulations.

Uploaded by

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

js Task

The document outlines various programming tasks involving conditional statements and loops, including functions for grade evaluation, day classification, arithmetic operations, month days, traffic light actions, and age groups. It also includes tasks for printing numbers, calculating sums, finding prime numbers, and manipulating arrays such as finding sums, reversing, and merging. Additionally, it covers array methods for adding, removing, and extracting elements, as well as string manipulations.

Uploaded by

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

Switch case and if else task

1. Grade Evaluator

Write a function getGradeRemark that takes a grade (A, B, C, D, E, F) as input and returns a
remark based on the following criteria:

● "A" → "Excellent"
● "B" → "Very Good"
● "C" → "Good"
● "D" → "Needs Improvement"
● "E" → "Poor"
● "F" → "Fail"
● Any other input → "Invalid Grade"

2. takes a day of the week as input (e.g., "Monday", "Saturday") and returns whether
it's a Weekday or a Weekend.

Conditions:

● "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" → "Weekday"


● "Saturday", "Sunday" → "Weekend"
● Any other input → "Invalid Day"

3. that takes two numbers and an operator (+, -, *, /) as input and returns the result of
the operation.

Conditions:

● "+" → Add the two numbers


● "-" → Subtract the second number from the first
● "*" → Multiply the two numbers
● "/" → Divide the first number by the second (if the second number is 0, return
"Cannot divide by zero")
● If the operator is invalid, return "Invalid operator"
4. takes a month name (e.g., "January", "February") as input and returns the
number of days in that month.

Conditions:

● "January", "March", "May", "July", "August", "October", "December" → 31


days
● "April", "June", "September", "November" → 30 days
● "February" → 28 or 29 days (Leap Year Consideration)
● Any other input → "Invalid Month"

5. takes a traffic light color as input ("red", "yellow", "green") and returns the
corresponding action:
● "red" → "Stop"
● "yellow" → "Caution"
● "green" → "Go"
● Any other input → "Invalid color"

6. takes a single letter (a-z or A-Z) as input and returns:


● "Vowel" if the letter is A, E, I, O, U (case-insensitive)
● "Consonant" for all other letters
● "Invalid character" if the input is not a letter

7. takes a number as input and returns:


● If the number is divisible by both 3 and 5, return "FizzBuzz".
● If the number is divisible by 3 only, return "Fizz".
● If the number is divisible by 5 only, return "Buzz".
● If the number is negative, return "Negative number".
● Otherwise, return "Not divisible by 3 or 5"

8. takes a person's age as input and returns their age group:

Conditions:
● If the age is less than 0, return "Invalid age".
● If the age is 0 to 12, return "Child".
● If the age is 13 to 19, return "Teenager".
● If the age is 20 to 35, return "Young Adult".
● If the age is 36 to 60, return "Adult".
● If the age is above 60, return "Senior Citizen"

9. takes a numeric score (0-100) as input and returns the corresponding grade based on
the following conditions:

Conditions:

● If the score is less than 0 or greater than 100, return "Invalid Score".
● 90 - 100 → "A"
● 80 - 89 → "B"
● 70 - 79 → "C"
● 60 - 69 → "D"
● 0 - 59 → "F"

For loop task

1. Print Numbers from 1 to 10


2. Print Even Numbers from 1 to 20
3. Print Multiplication Table of a Given Number
4. Sum of First N Natural Numbers
Input: 5
Output: 15 (1+2+3+4+5)
5. Factorial of a Number
6. Reverse Number Printing (10 -1)
7. Print the Fibonacci Series
8. Count Digits in a Number (316 - 3 digit)
9. Find Prime Numbers between 1 and 50
10. Reverse a Given Number (input - 321 output - 123)
11. Check if a Number is Palindrome
12. Find the Sum of Digits of a Number
13. Find Armstrong Numbers from 1 to 1000 (153 → (1³ + 5³ + 3³) = 153)
14. Check if a Number is Perfect
A number is Perfect if the sum of its factors (excluding itself) is equal to the
number.
Input: 6
Output: Perfect Number (1+2+3 = 6)
15. Find the Sum of Odd and Even Numbers from 1 to N

Array using for loop

1. Find the Sum of All Elements in an Array


Input: [1, 2, 3, 4, 5]
Output: 15
2. Find the Largest Number in an Array
Input: [10, 25, 35, 50, 40]
Output: 50
3. Find the Smallest Number in an Array
Input: [15, 5, 20, 8]
Output: 5
4. Count How Many Even and Odd Numbers are in an Array
Input: [2, 5, 6, 9, 10]
Output: Even: 3, Odd: 2
5. Reverse an Array
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
6. Find the Second Largest Number in an Array
Input: [12, 45, 67, 89, 34]
Output: 67
7. Array is Sorted in Ascending Order
Input : [9,3,7,1]
Output : [1,3,7,9]
8. Find Duplicate Elements in an Array
Input: [1, 2, 3, 2, 4, 5, 4]
Output: 2, 4
9. Merge Two Arrays
Input: [1, 2, 3], [4, 5, 6]
Output: [1, 2, 3, 4, 5, 6]
10. Remove Duplicates from an Array
Input: [1, 2, 2, 3, 4, 4, 5]
Output: [1, 2, 3, 4, 5]
11. Rotate an Array to the Left by One Position
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
12. Rotate an Array to the Right by One Position
Input: [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]

13. Find the Intersection of Two Arrays (Common Elements)


Input: [1, 2, 3, 4], [3, 4, 5, 6]
Output: [3, 4]

14. Find the Union of Two Arrays Without Duplicates


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

15. Find the Count of Positive and Negative Numbers in an Array


Input: [-5, 10, -2, 8, 15, -1]
Output: Positive: 3, Negative: 3

16. Swap the First and Last Elements of an Array

Input: [10, 20, 30, 40, 50]

Output: [50, 20, 30, 40, 10]

17. Find the Product of All Elements in an Array


Input: [1, 2, 3, 4]
Output: 24 (1 × 2 × 3 × 4)
18. Separate Even and Odd Numbers into Two Arrays
Input: [1, 2, 3, 4, 5, 6]
Output: Even: [2, 4, 6]
Odd: [1, 3, 5]
19. Move All Zeroes to the End of an Array
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

20. Find the Frequency of Each Element in an Array


Input: [1, 2, 2, 3, 3, 3]
Output: 1 → 1 time
2 → 2 times
3 → 3 times

21. Reverse the Order of Elements Without Using a New Array


Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
22. Find the First Repeated Element in an Array
Input: [5, 1, 2, 3, 2, 5]
Output: 2

Array Method Task

1. Input: arr = [10, 20, 30], add 40 at the end.


Expected Output: [10, 20, 30, 40]

2. Input: arr = [1, 2, 3, 4], remove the first element.


Expected Output: [2, 3, 4]

3. Input: arr = [5, 10, 15, 20], remove the last element.
Expected Output: [5, 10, 15]

4. Input: arr = [2, 3, 4], add 1 at the beginning.


Expected Output: [1, 2, 3, 4]

5. Input: arr = ['apple', 'banana'], add 'grape' at the end.


Expected Output: ['apple', 'banana', 'grape']

6. Input: arr = ['dog', 'cat'], add 'rabbit' at the beginning.


Expected Output: ['rabbit', 'dog', 'cat']

7. Input: arr = ['a', 'b', 'c', 'd'], remove the first element.
Expected Output: ['b', 'c', 'd']

8. Input: arr = ['red', 'blue', 'green'], remove the last element.


Expected Output: ['red', 'blue']

9. Input: arr = [1, 2, 3, 4], add 5 and 6 at the end.


Expected Output: [1, 2, 3, 4, 5, 6]

10. Input: arr = [100, 200, 300, 400, 500], remove the last two elements.
Expected Output: [100, 200, 300]

11. Input: arr = [10, 20, 30], add 5 and 0 at the beginning.
Expected Output: [5, 0, 10, 20, 30]
12. Input: arr = [100, 200, 300], remove the first two elements.
Expected Output: [300]

13. Input: arr = [1, 2, 3, 4], convert to string.


Expected Output: "1,2,3,4"

14. Input: arr = ['apple', 'banana', 'cherry'], convert to string.


Expected Output: "apple,banana,cherry"

15. Input: arr = [true, false, true], convert to string.


Expected Output: "true,false,true"
16. Input: arr = [10, 20, 30, 40, 50], extract [20, 30].
Expected Output: [20, 30]

17. Input: arr = ['a', 'b', 'c', 'd', 'e'], extract ['b', 'c', 'd'].
Expected Output: ['b', 'c', 'd']

18. Input: arr = [1, 2, 3, 4, 5], extract last two elements.


Expected Output: [4, 5]

19. Input: arr = [10, 20, 30, 40], remove 20 and 30.
Expected Output: [10, 40]

20. Input: arr = ['a', 'b', 'c', 'd'], remove 'c' and insert 'x', 'y'.
Expected Output: ['a', 'b', 'x', 'y', 'd']

21. Input: arr = [1, 2, 3, 4, 5], insert 100 at index 2.


Expected Output: [1, 2, 100, 3, 4, 5]

22. Input: arr = [1, [2, 3], [4, 5]], flatten one level.
Expected Output: [1, 2, 3, 4, 5]

23. Input: arr = [1, [2, [3, 4], 5]], flatten one level.
Expected Output: [1, 2, [3, 4], 5]

24. Input: arr = [[['a']], [['b']], 'c'], flatten one level.


Expected Output: [['a'], ['b'], 'c']
25. Input: arr1 = [1, 2, 3], arr2 = [4, 5, 6], merge both.
Expected Output: [1, 2, 3, 4, 5, 6]
26. Input: arr1 = ['apple', 'banana'], arr2 = ['cherry'], merge both.
Expected Output: ['apple', 'banana', 'cherry']

27. Input: arr1 = [10, 20], arr2 = [30, 40], arr3 = [50, 60], merge all.
Expected Output: [10, 20, 30, 40, 50, 60]
28. Input: "helloworld" (Extract hello and world manually)
Expected Output: ["hello", "world"]
29. Input: "applebanana" (Separate apple and banana)
Expected Output: ["apple", "banana"]
30. Input: "abcd1234efgh" (Separate letters and numbers)
Expected Output: ["abcd", "1234", "efgh"]
31. Input: ["hello", "world"]
Expected Output: "helloworld"
32. Input: ["apple", "banana", "grape"]
Expected Output: "applebananagrape"
33. Input: ["abc", "123", "xyz"]
Expected Output: "abc123xyz"
34. Input: arr = [10, 20, 30, 40, 50, 60]
Extract [30, 40] from the array.
Expected Output: [30, 40]
35. Input: arr = ['apple', 'banana', 'grape', 'mango', 'cherry']
Extract the first three elements.
Expected Output: ['apple', 'banana', 'grape']
36. Input: arr = ['red', 'blue', 'green', 'yellow', 'pink']
Extract the last two elements.
Expected Output: ['yellow', 'pink']
37. Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Extract every alternate element starting from index 1 ([2, 4, 6, 8, 10]).
Expected Output: [2, 4, 6, 8, 10]
38. Input: arr = ['a', 'b', 'c', 'd', 'e', 'f']
Extract everything except the first and last elements.
Expected Output: ['b', 'c', 'd', 'e']
39. Input: arr = [100, 200, 300, 400, 500]
Remove 300 and 400.
Expected Output: [100, 200, 500]
40. Input: arr = ['cat', 'dog', 'rabbit', 'elephant']
Replace 'rabbit' with 'lion'.
Expected Output: ['cat', 'dog', 'lion', 'elephant']
41. Input: arr = ['one', 'two', 'three', 'four']
Insert 'zero' at the beginning without removing any element.
Expected Output: ['zero', 'one', 'two', 'three', 'four']
42. Input: arr = [10, 20, 30, 40, 50]
Remove 20 and insert 15, 18 in its place.
Expected Output: [10, 15, 18, 30, 40, 50]
43. Input: arr = ['x', 'y', 'z']
Insert 'a', 'b', 'c' at index 1.
Expected Output: ['x', 'a', 'b', 'c', 'y', 'z']
44. Input: arr = [10, 20, 30, 40, 50, 60, 70]

Extract the middle three elements, regardless of array length.

Expected Output: [30, 40, 50]

45. Input: arr = [1, 2, 3, 4, 5]

Reverse the array without modifying the original.

Expected Output: [5, 4, 3, 2, 1]

46. Input: arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Extract every 2nd element from the array.
Expected Output: ['b', 'd', 'f', 'h']
47. Input: arr = [10, 20, 30, 40, 50, 60]
Extract only the second half of the array.
Expected Output: [40, 50, 60]
48. Input: arr = [100, 200, 300, 400, 500, 600, 700, 800, 900]
Extract the last 3 elements, no matter how long the array is.
Expected Output: [700, 800, 900]
49. Input: arr = [1, 2, 3, 4, 5, 6]

Modify the array so that the first two elements move to the end.

Expected Output: [3, 4, 5, 6, 1, 2]

50. Input: arr = [10, 20, 50, 60]


Insert [30, 40] between 20 and 50.
Expected Output: [10, 20, 30, 40, 50, 60]

51. Input: arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
Modify the array by removing elements at even indexes.

Expected Output: ['b', 'd', 'f']

52. Input: arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]

Modify the array by removing every 3rd element.

Expected Output: [10, 20, 40, 50, 70, 80]

53. Input: arr = [1, 2, 3, 4, 5, 6, 7, 8]

Modify the array so that the first half swaps with the second
half.

Expected Output: [5, 6, 7, 8, 1, 2, 3, 4]

Array Sort and Search

1. Input: [2, 4, 6, 8] (Double each element)


Expected Output: [4, 8, 12, 16]
2. Input: ["a", "b", "c"] (Convert each letter to uppercase)
Expected Output: ["A", "B", "C"]
3. Input: [1, 2, 3, 4, 5] (Square each element)
Expected Output: [1, 4, 9, 16, 25]
4. Input: [10, 15, 20, 25, 30] (Keep only even numbers)
Expected Output: [10, 20, 30]
5. Input: ["apple", "banana", "grape", "kiwi"] (Keep only words
with more than 5 letters)
Expected Output: ["banana", "grape"]
6. Input: [1, -2, 3, -4, 5] (Remove negative numbers)
Expected Output: [1, 3, 5]
7. Input: [1, 2, 3, 4] (Print each element)
Expected Output:
1
2
3
4

8. Input: ["A", "B", "C"] (Append "!" to each value and print)
Expected Output:

A!

B!

C!

9. Input: [5, 10, 15] (Add 5 to each element and print result)
Expected Output:

10

15

20

10. Input: [10, 20, 30, 40] (Check if 30 is present)

Expected Output: true

11. Input: ["apple", "orange", "mango"] (Check if "banana"


is present)
Expected Output: false
12. Input: [100, 200, 300, 400] (Check if 500 exists)

Expected Output: false

13. Input: [2, 4, 6, 8] (Find first even number greater


than 5)

Expected Output: 6

14. Input: ["apple", "banana", "kiwi"] (Find the first word


starting with "b")

Expected Output: "banana"


15. Input: [10, 20, 30, 40, 50] (Find the first number
greater than 25)
Expected Output: 30
16. Input: [2, 4, 6, 8] (Find index of first even number
greater than 5)
Expected Output: 2
17. Input: ["apple", "banana", "kiwi"] (Find index of first
word starting with "b")

Expected Output: 1

18. Input: [10, 20, 30, 40, 50] (Find index of first number
greater than 25)

Expected Output: 2

19. Input: [10, 20, 30, 40] (Find index of 30)

Expected Output: 2

20. Input: ["apple", "banana", "mango"] (Find index of


"banana")
Expected Output: 1
21. Input: [100, 200, 300, 400] (Find index of 500)

Expected Output: -1

22. Input: [2, 4, 6, 8] (Check if all are even)

Expected Output: true

23. Input: ["apple", "banana", "kiwi"] (Check if all start


with "a")
Expected Output: false
24. Input: [100, 200, 300] (Check if all are greater than
50)

Expected Output: true


25. Input: arr1 = [1, 2, 3], arr2 = [4, 5, 6]
Merge both arrays.
Expected Output: [1, 2, 3, 4, 5, 6]
26. Input: arr = ["A", "B", "C"]

Create a copy of the array.

Expected Output: ["A", "B", "C"]

27. Input: arr1 = [10, 20], arr2 = [30, 40], arr3 = [50,
60]
Merge all three arrays.
Expected Output: [10, 20, 30, 40, 50, 60]
28. Replace numbers with their squares if they are even,
else triple them

Input: [2, 3, 4, 5, 6]
Expected Output: [4, 9, 16, 15, 36]

29. Convert a list of names into their initials

Input: ["John Doe", "Alice Brown", "Charlie Puth"]


Expected Output: ["J.D", "A.B", "C.P"]

30. Convert array of objects into an array of formatted


strings

{ name: "Alice", age: 25 },

{ name: "Bob", age: 30 }

Expected Output: ["Alice (25)", "Bob (30)"]

31. Reverse each string in an array


Input: ["apple", "banana", "grape"]
Expected Output: ["elppa", "ananab", "eparg"]

32. Multiply each number by its index position

Input: [5, 10, 15, 20]


Expected Output: [0, 10, 30, 60]

33. Remove duplicate values dynamically

Input: [1, 2, 2, 3, 4, 4, 5]
Expected Output: [1, 2, 3, 4, 5]

34. Keep only words with vowels at the start and end

Input: ["apple", "orange", "banana", "umbrella"]


Expected Output: ["apple", "orange", "umbrella"]

35. Keep only numbers where the sum of digits is even

Input: [12, 34, 56, 77, 89]


Expected Output: [12, 34, 56]

36. Remove elements that contain any special characters

Input: ["hello", "world!", "test#", "okay"]


Expected Output: ["hello", "okay"]

37. Keep only the elements that appear more than once

Input: [1, 2, 2, 3, 3, 3, 4, 5]
Expected Output: [2, 2, 3, 3, 3]

38. Find if an array contains at least one negative number

Input: [5, 10, -3, 20, -1]


Expected Output: true

39. Check if all elements in one array exist in another


arr1 = [1, 2, 3];

arr2 = [3, 2, 1, 4, 5];

Expected Output : true

40. Find if any word from a banned list appears in a


sentence

sentence = "I love programming but hate bugs";

bannedWords = ["hate", "bad", "terrible"];

Expected Output: true

41. Find if an array contains only even numbers

Input: [2, 4, 6, 8]
Expected Output: true

42. Check if a number exists as a substring inside any


element

arr = ["apple42", "banana", "grape99"];

num = "42";

Expected Output: true

43. Count the number of vowels in an array of words

Input: ["apple", "banana", "grape"]


Expected Output: { a: 4, e: 2, i: 0, o: 0, u: 0 }

44. Find the longest word in an array

Input: ["apple", "banana", "watermelon", "grape"]


Expected Output: "watermelon"

45. Find the total price of items in a shopping cart


cart = [

{ name: "Shoes", price: 50 },

{ name: "Bag", price: 30 },

{ name: "Watch", price: 20 }

];

Expected Output: 100

46. Capitalize the first letter of each word

Input: ["hello", "world", "javascript"]


Expected Output: ["Hello", "World", "Javascript"]

47. Count how many times each number appears in an array

Input: [1, 2, 2, 3, 3, 3, 4]
Expected Output: { 1: 1, 2: 2, 3: 3, 4: 1 }

You might also like