JS Arrays Exercises
JS Arrays Exercises
Problems for in-class lab for the "JS Fundamentals - May 2019". Submit your solutions in the SoftUni
judge system at judge.softuni.bg/Contests/1243/Arrays-Lab.
Examples
Input Output
['20', '30', '40'] 60
['10', '17', '22', '33'] 43
['11', '58', '69'] 80
Hints
Use the Number() function
2. Day of Week
Write a program which receives a number and prints the corresponding name of the day of week.
If the number is NOT a valid day, print 'Invalid day!'.
Examples
Input Output
3 Wednesday
6 Saturday
11 Invalid day!
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 1 of 7
Hints
Examples
Input Output
3, [10, 20, 30, 40, 50] 30 20 10
4, [-1, 20, 99, 5] 5 99 20 -1
2, [66, 43, 75, 89, 47] 43 66
Hints
Use push() to add elements inside the new array
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 2 of 7
4. Reverse an Array of Strings
Write a program which receives an array of strings (space separated values). Your task is to reverse it
and print its elements. Swap elements.
Examples
Input Output Comments
The first element should be last, and the last
['a', 'b', 'c', 'd', 'e'] edcba
element should be first.
['abc', 'def', 'hig', 'klm', 'nop'] nop klm hig def abc
['33', '123', '0', 'dd'] dd 0 123 33
Hints
Loop to the half-length of the array
Create a function to swap two elements inside an array
Examples
Input Output
['1','2','3','4','5','6'] 12
['3','5','7','9'] 0
['2','4','6','8','10'] 30
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 3 of 7
Hints
Parse each string to number
Examples
Input Output Comments
[1,2,3,4,5,6] 3 2 + 4 + 6 = 12, 1 + 3 + 5 = 9, 12 - 9 = 3
[3,5,7,9] -24
[2,4,6,8,10] 30
Hints
Parse each string to number
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 4 of 7
Create two variables - for even and odd sum
Iterate through all elements in the array with for-of loop and check if the number is odd or
even
7. Equal Arrays
Write a program which receives two string arrays and print on the console whether they are identical or
NOT.
Arrays are identical if their elements are equal. If the arrays are identical find the sum of the first one
and print on the console following message:
'Arrays are identical. Sum: {sum}'
If the arrays are NOT identical find the first index where the arrays differ and print on the console
following message:
'Arrays are not identical. Found difference at {index} index'.
Examples
Input Output
['10','20','30'], ['10','20','30'] Arrays are identical. Sum: 60
['1','2','3','4','5'], ['1','2','4','4','5'] Arrays are not identical. Found difference at 2 index
['1'], ['10'] Arrays are not identical. Found difference at 0 index
Hints
First, we receive two arrays of strings and parse them.
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 5 of 7
Iterate through the arrays and compare all element. If the elements are NOT equal print the
required message and break the loop.
Examples
For example, if we have 3 elements [2, 10, 3], we sum the first two and the second two elements and
obtain {2+10, 10+3} = {12, 13}, then we sum again all adjacent elements and obtain {12+13} = {25}.
Hints
While we have more than one element in the array nums[], repeat the following:
Allocate a new array condensed[] of size nums.Length-1.
Sum the numbers from nums[] to condensed[]:
o condensed[i] = nums[i] + nums[i+1]
nums[] = condensed[]
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 6 of 7
The process is illustrated below:
© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 7 of 7