import Head from 'next/head' import React, { useState } from "react"; // Utilities Array.prototype.containsArray = function(val) { var hash = {}; for(var i=0; i { let l = arr.length let isOrdered = true for(let i=0; i { let element1Greater = newComparisons.containsArray([element1, element2]) let element2Greater = newComparisons.containsArray([element2, element1]) if(element1Greater || element2Greater){ return element1Greater && !element2Greater } else{ setToComparePair([element1, element2]) console.log(`No comparison found between ${element1} and ${element2}`) console.log(`Comparisons:`) console.log(JSON.stringify(newComparisons, null, 4)); return "No comparison found" } } function merge(newComparisons, left, right) { let sortedArr = []; // the sorted elements will go here while (left.length && right.length) { // insert the biggest element to the sortedArr let comparison = compareTwoElements(newComparisons, left[0], right[0]) if(comparison == "No comparison found"){ return "No comparison found; unable to proceed" } else if (comparison) { // left[0] > right[0] sortedArr.push(left.shift()); } else { sortedArr.push(right.shift()); } } // use spread operator and create a new array, combining the three arrays return [...sortedArr, ...left, ...right]; // if they don't have the same size, the remaining ones will be greater than the ones before } function mergeSort(arr, newComparisons) { if(arr == "No comparison found; unable to proceed"){ return "No comparison found; unable to proceed" } const half = arr.length / 2; // the base case is array length <=1 if (arr.length <= 1) { return arr; } const left = arr.splice(0, half); // the first half of the array const right = arr; let orderedFirstHalf = mergeSort(left, newComparisons) let orderedSecondHalf = mergeSort(right, newComparisons) if(orderedFirstHalf != "No comparison found; unable to proceed" && orderedSecondHalf != "No comparison found; unable to proceed"){ let result = merge(newComparisons, orderedFirstHalf, orderedSecondHalf); return result }else{ return "No comparison found; unable to proceed" } } let nextStep = (comparisons, element1, element2) => { console.log("Comparisons: ") console.log(JSON.stringify(comparisons, null, 4)); let newComparison = [element1, element2] // [element1, element2] let newComparisons = [...comparisons, newComparison] console.log("New comparisons: ") console.log(JSON.stringify(newComparisons, null, 4)); updateComparisons(newComparisons) let result = mergeSort(list, newComparisons) console.log(result) console.log(toComparePair) if(result != "No comparison found; unable to proceed" && checkIfListIsOrdered(result, newComparisons)){ alert("Order complete") console.log(result) setIsListOrdered(true) setOrderedList(result) } } // Html return (
Welcome to Hot Or Not

Hot or not?

{`Ordered list: ${JSON.stringify(orderedList, null, 4)}`}

{`Comparisons: ${JSON.stringify(comparisons, null, 4)}`}

) }