JavaScript Arrays

Write a JavaScript program to find the sum and average of array elements

Description: Find the sum and the average of the array [10, 20, 30, 40].

Hint:

  • Use reduce() to add all items.
  • Average = sum / number of items.

JavaScript program for sum and average of an array

const numbers = [10, 20, 30, 40];

const sum = numbers.reduce((total, n) => total + n, 0);
const average = sum / numbers.length;

console.log("Sum:", sum);
console.log("Average:", average);

Output:

Tutorials Class - Output Window

Sum: 100
Average: 25

How it works

  • reduce() starts with 0 and adds each number.
  • The average is the sum divided by numbers.length.

Read Array Methods.

Write a JavaScript program to find the largest and smallest number in an array

Description: Find the largest and smallest number in [45, 12, 89, 3, 67].

Hint:

  • Math.max takes separate numbers, so spread the array with ....

JavaScript largest and smallest in an array

const numbers = [45, 12, 89, 3, 67];

console.log("Largest:", Math.max(...numbers));
console.log("Smallest:", Math.min(...numbers));

// with a loop
let max = numbers[0];
let min = numbers[0];
for (const n of numbers) {
  if (n > max) max = n;
  if (n < min) min = n;
}
console.log(max, min);

Output:

Tutorials Class - Output Window

Largest: 89
Smallest: 3
89 3

How it works

  • ...numbers spreads the array into separate arguments.
  • The loop keeps the biggest and smallest value seen so far.

Write a JavaScript program to remove duplicates from an array

Description: Remove the duplicates from [1, 2, 2, 3, 4, 4, 5].

Hint:

  • A Set keeps only unique values.

JavaScript program to remove duplicates

const items = [1, 2, 2, 3, 4, 4, 5];

const unique = [...new Set(items)];
console.log(unique);

const unique2 = items.filter((x, index) => items.indexOf(x) === index);
console.log(unique2);

Output:

Tutorials Class - Output Window

[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]

How it works

  • new Set(items) removes duplicates, and [...set] turns it back to an array.
  • The second way keeps an item only if its first position is the current position.

Read Map and Set.

Write a JavaScript program to filter even numbers and double them

Description: From [1, 2, 3, 4, 5, 6] keep only the even numbers and double each one.

Hint:

  • Use filter() first, then map().

JavaScript filter and map program

const numbers = [1, 2, 3, 4, 5, 6];

const result = numbers
  .filter(n => n % 2 === 0)
  .map(n => n * 2);

console.log(result);

Output:

Tutorials Class - Output Window

[ 4, 8, 12 ]

How it works

  • filter() keeps 2, 4 and 6.
  • map() doubles them to 4, 8 and 12.

Read Array Methods.

Write a JavaScript program to count the frequency of words in a sentence

Description: Count each word of the sentence “to be or not to be”.

Hint:

  • Split the sentence into words.
  • Use an object where the word is the key and the count is the value.

JavaScript word frequency program

const sentence = "to be or not to be";
const counts = {};

for (const word of sentence.split(" ")) {
  counts[word] = (counts[word] || 0) + 1;
}

console.log(counts);

Output:

Tutorials Class - Output Window

{ to: 2, be: 2, or: 1, not: 1 }

How it works

  • counts[word] || 0 gives 0 for a new word, and then 1 is added.
  • Objects keep the order in which keys were added.