This code represents an HTML document containing a JavaScript implementation....

February 4, 2025 at 06:27 PM

<html><title>Sort</title> <head> <script type="text/javascript"> function initialize() { var tempdata = new Array (-21, -14, -13, -9, -12, -16, -16, -15, -13, -10, -5, -5, -4, -2) //set n equal to the number of values in the array var n = 14; //write a statement for the unsorted data document.write('<h3>Unsorted 14 day temperature forecast </h3> '+ tempdata); // call sort function sortdata(n,tempdata); //write a statement for the sorted data document.write('<h3>Sorted 14 day temperature forecast </h3> '+ tempdata); } </script> <script type="text/javascript"> function sortdata(n,data) { var i, j, swapped for(i = 0; i < n; i++) { var swapped = false; for(j = 0; j < (n-1); j++) { if(data[j] > data[j+1]) { // Chk if variables need swapping swap = data[j+1]; // The following 3 lines swap the variables data[j+1] = data[j]; data[j] = swap; swapped = true; } } if (swapped==false) break;} // if the swapped flag isn't changed then there is return data} //point continuing as all values are in order </script> </head> <body onLoad="initialize()"> <br> <a href="https://classes.uleth.ca/classwork/202501GEOG4700L01/ryan.kettlewell/Lab2/Lab2.html"> </body> </html>

This code represents an HTML document containing a JavaScript implementation. Here’s what it does:

Functionality

  • The webpage initializes by loading the initialize function when the <body> is loaded.
  • The initialize function works with an array of integers (tempdata) representing 14-day temperature forecasts, sorts it, and displays the results (unsorted and sorted) on the webpage.

Key Steps in the Code

  1. Define the Data:

    • tempdata is an array containing 14 integer values (negative temperatures).
    • n is set to 14, representing the number of elements in the array.
  2. Display Unsorted Data:

    • The unsorted data (tempdata) is directly written to the webpage using document.write.
  3. Sort the Data:

    • The function sortdata is called with parameters n (length of the array) and tempdata (data itself).
    • Inside sortdata, a sorting algorithm (a bubble sort) is used:
      • It loops through the array multiple times.
      • During each pass, it compares adjacent elements and swaps them if the earlier element is greater than the next.
      • The swapped flag is used to check if any elements were swapped during a pass; if not, the sorting process ends early, as the array is already sorted.
  4. Display Sorted Data:

    • Once sorted by the sortdata function, the ordered array is written to the webpage using document.write.
  5. Output on the Webpage:

    • Two headings and accompanying arrays are displayed:
      • Unsorted 14-day Temperature Forecast
      • Sorted 14-day Temperature Forecast

Summary of What the Code Does

The code:

  1. Initializes an array (tempdata) with 14 numerical temperature values.
  2. Shows the array in its unsorted order on a webpage.
  3. Sorts the array in ascending order using a bubble-sort algorithm.
  4. Displays the sorted array on the webpage.

This program is a simple example of JavaScript array manipulation, bubble sorting, and interacting with HTML to display data on a webpage.


Note

The bubble sort is not the most efficient sorting algorithm, especially for large data sets, but it works here given the limited size of the array (14 elements).

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node