Array Challenges

Think you have a good handle on arrays? Why not try out these challenges then?

1 - First Element

Write a function to get the first element in an array. If you get that, try to find the last one.

  • Sample Data

    var arr=[ 'We', 'love', 'learning', 'JavaScript!','It', 'is', 'a', 'fantastic', 'language' ]
  • Expected Output

    • First

      We

    • Last

      language

2 - Most Frequent Element

Calculate the most frequent element in an array.

  • Sample Data

    var arr=[ 1, 'fish', 2, 'fish', 'red', 'fish', 'blue', 'fish'];
  • Expected Output

    fish ( 4 times )

3 - Largest and Smallest

Return the largest number in an array. Too easy? Try finding the largest numbers in multiple arrays. If that's still too easy, try doing the same with the smallest number.

  • Sample Data

    • Single Array

      var arr= [ 68, 47, 85, 22]
    • Multiple Arrays

      var arr= [[ 68, 47, 85, 22], [86, 75, 30, 9], [482, 829, 147, 643], [3906], [2222], [1337], [4962]]
  • Expected Output

    • Largest

      85

      [85, 86, 829, 4962]

    • Smallest

      22

      [22, 9, 147, 1337]

4 - Missing Number

There's a number missing from this unsorted array, what is it?

  • Sample Data

    var arr = [ 14, 5, 7, 4, 12, 1, 2, 10, 6, 8, 15, 3, 13, 9 ];
  • Expected Output

    11

5 - "Arrays" in the DOM

Given the following HTML, change all <p> tags to blue and italics.

  • Sample Data

    <h1>DOM CHALLENGE!</h1>
    <p>I believe in you!</p>
    <h4>You can do it!</h4>
    <p>Just Think</p>
    <h1>Well, what's your answer?</h1>
    <button onClick="myFunction()">Click me!</button>
  • Expected Output

6 - Sorting

Find two different ways to sort the given array so that the element with the lowest value is on the far left and the element with the highest value is on the far right.

  • Sample Data

    [207, 150, 71, -25, 369, 246, -13, 22, 150]
  • Expected Output

    [ -25, -13, 22, 71, 150, 150, 207, 246, 369 ]

Last updated