A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Sleep Sort in Javascript page! Here, you'll find the source code for this program as well as a description of how the program works.
function sleepSort(arr) {
const sortedArray = [];
function sleepSortHelper(item) {
setTimeout(() => {
sortedArray.push(item);
if (sortedArray.length === arr.length) {
console.log(sortedArray.join(", "));
}
}, item*1000);
}
arr.forEach((item) => {
if (item < 0) {
console.log(error_msg);
return;
}
sleepSortHelper(item);
});
}
const error_msg =
'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"';
function sortNumbers(input) {
const numberList = input.split(",").map((item) => parseInt(item.trim()));
if (numberList.length < 2) {
console.log(error_msg);
} else if (numberList.some(isNaN)) {
console.log(error_msg);
} else {
sleepSort(numberList);
}
}
const input = process.argv[2];
if (!input) {
console.log(error_msg);
} else {
sortNumbers(input);
}
Sleep Sort in Javascript was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.