Simple JS Test Solution

by Kashif Imran

HTML

<!-- You must not make any changes to the HTML section. -->
<div id='number1'>It should show the string "number1" after 1 sec</div>
<div id='number2'>It should show the string "number2" after 2 sec</div>
<div id='number3'>It should show the string "number3" after 3 sec</div>
<div id='number4'>It should show the string "number4" after 4 sec</div>
<!--Use the results DIV to show your output from different Tasks-->
<div id='results'></div>

JavaScript

// NOTE: Do NOT change the arrays and their contents given below. Only work on the tasks A-D using as much of existing code as possible

var arr1 = ['0zzjj', '1299bbkk', '32aaff', '122ddoo'];

var items = [{
  id: 10,
  key: 'a',
  numbers: [1, 2, 3],
  score: [10, 20, 30],
}, {
  id: 2,
  key: 'b',
  numbers: [3, 2, 3],
  score: [30, 20, 30],
}, {
  id: 3,
  key: 'c',
  numbers: [9, 3, 2],
  score: [90, 30, 20]
}];

/*
TASK-A:
Implement the following 'for' loop, so that the value of each div in the HTML section shows the number in its ID after the same number of seconds. For example, the text of <div> with ID='number1' will become "number1" after 1 second, 'number2' will become "number2" after 2 seconds and so on.
*/

for (let i = 0; i < 4; i++) {
	// TODO: Your code for TASK-A  
}

/*
TASK-B:
Implement the following function so that it sorts a given array of objects using one of its fields. The function should be able to sort any array of objects with field names other than what's in the example array.
*/

// arr = array of objects
// prop = name of the field/property to be used for sorting
// direction = either 1 for ascending or -1 for descending
// returns the same array
function sort(arr, prop, direction = 1) {
  // TODO: Your code here
}

// Create the follwing examples of the sort() function. Display your results using the showArrayResult() function
//1. sort by 'id' in ascending order
//2. sort by 'id' in descending order
//3. sort by 'key' in ascending order
//4. sort by 'key' in descending order

/*
TASK-C:
Implement the following function to sort the above array 'items' by the sum of its field 'numbers'.
// for param defition see sort()
// returns the same array
*/
function sortBySum(arr, prop, direction = 1) {
  // TODO: Your code here
}

// Create the follwing examples of the sortBySum() function. Display your results using the showArrayResult() function
//1. sort by 'numbers' in ascending order
//2. sort by 'numbers' in descending order
//3. sort...