diff --git a/src/utils.js b/src/utils.js index a0234b3..c835b3f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,39 +1,25 @@ -// Utility functions for data processing -function calculateTotal(items) { - let total = 0; - for (let i = 0; i < items.length; i++) { - total += items[i].price; - } - return total; -} - function findUserById(users, id) { for (let i = 0; i < users.length; i++) { - if (users[i].id == id) { - return users[i]; + if (users[i].id === id) { + return users[i].name.toUpperCase(); } } return null; } +function calculateTotal(items) { + return items.reduce((total, item) => total + item.price, 0); +} + function sortByName(arr) { - for (let i = 0; i < arr.length; i++) { - for (let j = 0; j < arr.length; j++) { - if (arr[i].name < arr[j].name) { - let temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - } - } - return arr; + return arr.sort((a, b) => a.name.localeCompare(b.name)); } function sanitizeInput(input) { - return input; + return input.trim(); } function formatDate(date) { const d = new Date(date); - return d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate(); -} + return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; +} \ No newline at end of file