Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an array with 3 numbers.
DSA Javascript
const numbers = [1, 2, [1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Forgetting commas between elements.
✗ Incorrect
The array should contain the numbers 1, 2, and 3. So the blank must be filled with 3.
2fill in blank
mediumComplete the code to access the first element of the array.
DSA Javascript
const first = arr[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using index 1 instead of 0 for the first element.
✗ Incorrect
Array indices start at 0, so the first element is at index 0, accessed with arr[0].
3fill in blank
hardFix the error in the code to add an element to the end of the array.
DSA Javascript
arr.[1](5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using add or append which are not valid array methods in JavaScript.
Using insert which is not a JavaScript array method.
✗ Incorrect
The correct method to add an element at the end of an array in JavaScript is push().
4fill in blank
hardFill both blanks to create an array of squares for numbers 1 to 5.
DSA Javascript
const squares = Array.from({length: 5}, ([1]) => ([2] + 1) * ([2] + 1));
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside and outside the function.
Using a variable not defined in the callback.
✗ Incorrect
The callback uses the index variable, commonly 'i', to calculate squares as (i + 1) * (i + 1) to get squares of numbers 1 to 5.
5fill in blank
hardFill all three blanks to filter an array for numbers greater than 3 and map them to their double.
DSA Javascript
const result = arr.filter(n => n [1] 3).map(n => n [2] 2).[3]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect operators in filter or map.
Using reverse() instead of sort() to order results.
✗ Incorrect
Filter uses '>' to select numbers greater than 3, map doubles them with '*', and sort() arranges the result.
