📖 Scenario: Imagine you have a list of daily tasks arranged in order. Sometimes, you want to rotate this list so that the tasks start from a different point, like moving the first few tasks to the end. This helps you plan your day differently without changing the order of tasks themselves.
🎯 Goal: You will create a program that rotates an array of tasks to the left by a given number of positions. This means the first few tasks move to the end, and the rest shift forward.
📋 What You'll Learn
Create an array called tasks with these exact values: 'Email', 'Meeting', 'Coding', 'Review', 'Lunch'
Create a variable called rotateBy and set it to 2
Write a function called rotateArrayLeft that takes an array and a number, and returns the array rotated to the left by that number
Print the rotated array using console.log
💡 Why This Matters
🌍 Real World
Rotating arrays is useful in scheduling, gaming, and data processing where order changes but elements remain the same.
💼 Career
Understanding array manipulation is fundamental for software development, especially in frontend and backend programming.
Progress0 / 4 steps
1
Create the initial array of tasks
Create an array called tasks with these exact values in order: 'Email', 'Meeting', 'Coding', 'Review', 'Lunch'
DSA Javascript
Hint
Use square brackets [] to create an array and separate items with commas.
2
Set the number of positions to rotate
Create a variable called rotateBy and set it to the number 2
DSA Javascript
Hint
Use const to create a variable that does not change.
3
Write the function to rotate the array left
Write a function called rotateArrayLeft that takes two parameters: arr (an array) and positions (a number). The function should return a new array rotated to the left by positions. Use array slicing and concatenation.
DSA Javascript
Hint
Use slice to get parts of the array and concat to join them back.
4
Print the rotated array
Use console.log to print the result of calling rotateArrayLeft with tasks and rotateBy as arguments.
DSA Javascript
Hint
Call the function with tasks and rotateBy, then print the result.
Practice
(1/5)
1. What does rotating an array to the right by 1 position mean? arr = [1, 2, 3, 4] rotated right by 1 becomes?
easy
A. [4, 1, 2, 3]
B. [2, 3, 4, 1]
C. [1, 2, 3, 4]
D. [3, 4, 1, 2]
Solution
Step 1: Understand right rotation by 1
Right rotation by 1 means the last element moves to the front, and others shift right.
Step 2: Apply to given array
Last element 4 moves to front, rest shift right: [4, 1, 2, 3].
Final Answer:
[4, 1, 2, 3] -> Option A
Quick Check:
Right rotate by 1 = last element front [OK]
Hint: Last element moves front on right rotation by 1 [OK]
Common Mistakes:
Confusing left and right rotation
Shifting elements left instead of right
Not moving last element to front
2. Which of the following is the correct JavaScript syntax to rotate an array arr to the left by 1 position?
easy
A. arr.unshift(arr.pop())
B. arr.push(arr.shift())
C. arr.pop(arr.unshift())
D. arr.shift(arr.push())
Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means first element moves to the end.
Step 2: Match syntax to operation
arr.shift() removes first element, arr.push() adds it to end. So arr.push(arr.shift()) rotates left by 1.
Final Answer:
arr.push(arr.shift()) -> Option B
Quick Check:
Left rotate = shift then push [OK]
Hint: Left rotate = shift first, then push it back [OK]