Skip to content

3.8 Some Practice

isArray()

Task 1: check if numbers is an array. Hint: use Array.isArray(numbers).

var numbers = [1, 5, 9, 10];

console.log(Array.isArray(numbers));

The result is: true

Task 2: Check if names is an array. Hint: use Array.isArray(names).

var names = "Nawras Ali";

console.log(Array.isArray(names));

The result is: false

Task 3: Check if mark is an array. Hint: use Array.isArray(mark).

var mark = 200;

console.log(Array.isArray(mark));

The result is: false

Task 4: Check if students is an array. Hint: use Array.isArray(students).

var students = ["Jojo"];

console.log(Array.isArray(students));

The result is: true

Array.isArray()

Array.isArray(arr) returns true if arr is an array. Otherwise; it returns false.

Task 5: If numbers is an array, return the minimum value in it. Otherwise; return numbers value.

var numbers = [9, 7, 14, 80, 19];

var res = Array.isArray(numbers) ? Math.min(...numbers) : numbers;

console.log(res);

The result is: 7

Task 6: If mark is an array, return the sum of its values. Otherwise; return 25 added to mark.

var mark = 800;

var res = Array.isArray(mark) ? mark.reduce((sum, val) => sum + val) : mark + 25;

console.log(res);

The result is: 825

Task 7: If mark is an array, multiply every value of it by 5. Otherwise; return mark.

var mark = [800];

var res =  Array.isArray(mark) ? mark.map(val => val * 5) : mark;

console.log(res);

The result is: [4000]

Task 8: If numbers is an array, find the sum of its values.

var numbers = [5, 8, 10];

if(Array.isArray(numbers)){
  var res = numbers.reduce((sum, val)=> sum + val);
  console.log(res);
}

The result is: 23

reverse()

Task 9: If salaries is an array, display the reversed array of it. Hint: use salaries.reverse()

var salaries = [100, 500, 700, 200];

if(Array.isArray(salaries)){
  var reversedArr = salaries.reverse();

}
console.log(reversedArr);

The result is: (4) [200, 700, 500, 100]

Task 10: Reverse the following array.

var arr = ["Hello", "world", "in", "reverse"];

console.log(arr.reverse())

The result is: (4) ["reverse", "in", "world", "Hello"]

Task 11: Reverse the following array, and display both the original and reversed arrays in the console.

var saying = ["You", "can", "do", "it"];

var sayingReversed = saying.reverse();

console.log("Original array: " + saying);
console.log("Reversed array: " + sayingReversed);

The result is as follows:

Original array: it,do,can,You
Reversed array: it,do,can,You

arr.reverse()

arr.reverse() returns the reversed array of arr.

arr.reverse() does change the original array.

Task 12: Reverse the following array without changing the original array.

var saying = ["You", "can", "do", "it"];

var sayingCopy = [...saying];
var sayingCopyReversed = sayingCopy.reverse();

console.log("Original array: " + saying);
console.log("Reversed array: " + sayingCopyReversed);

The result is as follows:

Original array: You,can,do,it
Reversed array: it,do,can,You

Task 13: Reverse the following array without changing the original array. Then, add 2 to every value of the reversed array. Display the sum of the resultant array values.

var arr = [5, 7, 9, 10, 0, 15];

var res = [...arr].reverse().map(val=>val+2).reduce((sum, val)=> sum+val);

console.log(res);

The result is: 58

Task 14: Reverse the following array without changing the original array. Then, divide every value of the resultant array by 10. Find the minimum and the maximum values of the resultant array.

var arr =  [1, 2, 3, 4, 5, 6];

var minValue = Math.min(...[...arr].reverse().map(val=>val/10));
var maxValue = Math.max(...[...arr].reverse().map(val=>val/10));

console.log(minValue, maxValue);

The result is: 0.1 0.6

sort()

Task 15: Sort the following array in ascending order. Hint: use arr.sort().

var numbers = [7, 9, 1, 0, 6, 5, 8, 4, 2, 3];

var sortedNumbers = numbers.sort();

console.log(sortedNumbers);

The result is: (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Task 16: Sort the following array in ascending order.

var words = ["Elephant", "Zebra", "Tiger", "Lion"];

var wordsSorted =  words.sort();

console.log(wordsSorted);

The result is: (4) ["Elephant", "Lion", "Tiger", "Zebra"]

Task 17: Sort the following array, and display both the original and sorted arrays in the console.

var quote = ["Arise", "awake", "and", "don't", "stop", "until", "the", "goal", "is", "reached."];

var quoteSorted = quote.sort();

console.log("Original array: " + quote);
console.log("Sorted array: " + quoteSorted);

The result is as follows:

Original array: ["Arise", "awake", "and", "don't", "stop", "until", "the", "goal", "is", "reached."]

Sorted array: ['Arise', 'and', 'awake', "don't", 'goal', 'is', 'reached.', 'stop', 'the', 'until']

arr.sort()

arr.sort() returns the sorted array.

arr.sort() does change the original array.

Task 18: Sort the following array without changing the original array.

var arr = [8, 7, 6, 0, 10];

var sortedArr = [...arr].sort();

console.log("Original array: " + arr);
console.log("Sorted array: " + sortedArr);

The result is as follows:

Original array: 8,7,6,0,10
Sorted array: 0,10,6,7,8

Task 19: Sort the following array without changing the original one. Then, multiply every value of the resultant array by 2.5.

var arr = [8, 10, 6, 78, 10];

console.log([...arr].sort().map(value=>value*2.5));

The result is: (5) [25, 25, 15, 195, 20]

Task 20: Sort the following array without changing the original one. Then, add 1 to every value of the resultant array, and find the average of the resultant array.

var arr = [80, 47, 46, 25];

var avg = ([...arr].sort().map(val=>val+1).reduce((sum,val)=>val+sum))/arr.length;

console.log(avg);

The result is: 50.5

Task 21: If values is an array, perform the following tasks:

  1. Sort the values without changing the original array.
  2. Save the values that are greater than 50 in highValues array.
  3. Save the values that are less than or equal to 50 in lowValues array.
  4. If some values in lowValues are equal to zero, add 1 to all lowValues values.
  5. If all the values in highValues are equal to 70, divide all highValues by 5.
  6. Then, join lowValues and highValues together.
  7. Reverse the order of the resultant array.
  8. Find the average of the resultant array.
var values = [50, 70, 5, 25, 0, 0, 12, 100, 80, 80, 88, 90, 13, 14, 44];

if (Array.isArray(values)){
  var valuesSorted = [...values].sort();
  var highValues = valuesSorted.filter(value=>value>50);
  var lowValues = valuesSorted.filter(value=>value<=50);

  if(lowValues.some(val=>val===0)){
    lowValues = lowValues.map(val=>val+1);
  }

  if(highValues.every(val=>val===70)){
    highValues = highValues.map(val=>val/5);
  }

  var newArr = [...lowValues, ...highValues].reverse();

  var avg = newArr.reduce((val, sum)=>val+sum)/newArr.length;

  console.log(newArr);
  console.log(avg);
}

The result is as follows:

(15) [90, 88, 80, 80, 70, 100, 51, 6, 45, 26, 15, 14, 13, 1, 1]
45.333333333333336