0
0
Javascriptprogramming~10 mins

Reduce method in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sum all numbers in the array using reduce.

Javascript
const numbers = [1, 2, 3, 4];
const total = numbers.reduce((acc, curr) => acc [1] curr, 0);
console.log(total);
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to provide the initial value 0.
2fill in blank
medium

Complete the code to find the product of all numbers using reduce.

Javascript
const nums = [2, 3, 4];
const product = nums.reduce((acc, curr) => acc [1] curr, 1);
console.log(product);
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using addition instead of multiplication.
Not setting the initial value to 1.
3fill in blank
hard

Fix the error in the reduce function to concatenate all strings in the array.

Javascript
const words = ['I', 'love', 'coding'];
const sentence = words.reduce((acc, curr) => acc [1] curr);
console.log(sentence);
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using arithmetic operators other than + for strings.
Not providing an initial value, which can cause errors with empty arrays.
4fill in blank
hard

Fill both blanks to create an object counting how many times each word appears.

Javascript
const words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
const count = words.reduce((acc, curr) => {
  acc[curr] = (acc[curr] [1] 0) [2] 1;
  return acc;
}, {});
console.log(count);
Drag options to blanks, or click blank then click option'
A||
B+
C-
D*
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using arithmetic operators instead of logical OR for default value.
Forgetting to add 1 to increment the count.
5fill in blank
hard

Fill all three blanks to create a new object with uppercase keys and values doubled if greater than 10.

Javascript
const data = {a: 5, b: 15, c: 8};
const result = Object.entries(data).reduce((acc, [1]) => {
  const [key, value] = [2];
  if (value [3] 10) {
    acc[key.toUpperCase()] = value * 2;
  }
  return acc;
}, {});
console.log(result);
Drag options to blanks, or click blank then click option'
Aentry
C>
Ditem
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different variable names inconsistently.
Using wrong comparison operators like < or ==.