JSFiddle - React, Tailwind, and code Playground
by Hugo Carneiro
JavaScript
let numbers = prompt('Please enter at least 3 random positive numbers, separated by comma');
let promise = new Promise((res, rej) => {
numbers ? res(numbers) : rej(console.log('Error'));
});
// Transform a prompt string into a JSON array
promise.then(res => {
const separator = /\s*(?:,|$)\s*/;
return res.split(separator);
})
// Chech if we got a JSON array
.then((res, rej) => {
return Array.isArray(res) ? res : rej(console.log('Not an array'));
})
// Sort numbers asc
.then(res => {
let sortedArray = res.sort();
return console.log(sortedArray);
});
//Valid JSON object examples (not very suitable for this case though):
/* {"person": {
"id": "1",
"name": "Marina",
"data": {
"favouriteMovies": [
{"value": "Movie1" },
{"value": "Movie2" },
{"value": "Movie3" }
]
}
}} */