JSFiddle - React, Tailwind, and code Playground

by Hugo Carneiro

JavaScript

/**
 * To test the code, press the run button and when
 * prompted, type a valid JSON array like the following:
 * [45, 53, 1, 33]
 *
 * Results will be logged to the browser console
 */
 
 
 
function sort(input) {
	return Promise.resolve(input.sort((a, b) => a - b));
}

function run() {
	let data;
  
	try {
		data = JSON.parse(prompt('Please type a JSON array'));
  } catch (e) {
  	return Promise.reject('Input is not a valid JSON');
  }
  
  if (!Array.isArray(data)) {
  	return Promise.reject('Input is not a valid array');
  }
  
  return sort(data);
}

run()
	.then(console.log)
  .catch(console.error);