are there any bugs in the following code? const HEADER =…
- User
are there any bugs in the following code? const HEADER = document.querySelector('header') const FORM = document.querySelector('#form') const TABLE = document.querySelector('#table') const ACTIONS_WRAP = document.querySelector('.actions-wrap') const API_URL = '/data.json'
let LAST_SAVED_DATA = []; let UI_DATA = []; let IS_UNSAVED_CHANGES = false; const SELECTED_ROW = { id: null, data: null, html: null }
async function fetchData(url) { try { const res = await fetch(url); const json = await res.json(); return json } catch (e) { console.log(e) return [] } }
function getUIDataItemIndex(id) { return UI_DATA.findIndex(el => el.id === id) }
function swapArrayIndex(arr, index1, index2) { const temp = arr[index1] arr[index1] = arr[index2] arr[index2] = temp }
function swapRowIndex(row1, row2) { const index1 = row1.children[1].textContent const index2 = row2.children[1].textContent
row1.children[1].textContent = index2 row2.children[1].textContent = index1}
function saveData() { LAST_SAVED_DATA = JSON.parse(JSON.stringify(UI_DATA)) }
function resetSelectionData() { SELECTED_ROW.data = null SELECTED_ROW.html = null }
function refreshUIData() { UI_DATA = JSON.parse(JSON.stringify(LAST_SAVED_DATA)) }
function sortUIData(property, dir) { UI_DATA.sort((a, b) => { if (dir === 'asc') { return a[property].localeCompare(b[property], undefined, { numeric: true }); } else { return b[property].localeCompare(a[property], undefined, { numeric: true }); } }); }
function populateForm(data) { FORM.chemical_name.value = data.chemical_name; FORM.vendor.value = data.vendor; FORM.density.value = data.density; FORM.viscosity.value = data.viscosity; FORM.packaging.value = data.packaging; FORM.pack_size.value = data.pack_size; FORM.unit.value = data.unit; FORM.quantity.value = data.quantity; }
function toggleForm() { HEADER.classList.toggle('show-form')
removeAllModes() if (HEADER.classList.contains('show-form')) { formMode() } else { if (SELECTED_ROW.data) { selectedMode() } else { idealMode() } }}
function createCell(text) { const cell = document.createElement('td'); cell.textContent = text; return cell }
function createRow(obj, index) { const row = document.createElement('tr');
const selectBox = createCell(`\u2714`); selectBox.classList.add('select-box'); row.appendChild(selectBox); row.dataset.id = obj['id'] const rowIndexCell = createCell(index); row.appendChild(rowIndexCell) for (const key in obj) { if (key === 'id') { continue } const cell = createCell(obj[key]) cell.dataset.property = key row.appendChild(cell); } return row}
function createTableBody(arrayOfObjs) { const fragment = document.createDocumentFragment();
- Assistant
I can help review the provided JavaScript code for potential issues. While I won't run the code, I can point out logical errors, common mistakes, or possible improvements based on my analysis.
-
getUIDataItemIndexfunction:- You are passing the full data object to
getUIDataItemIndexwhen calling it with in function, instead of passing the . This may result in incorrect index returned or a runtime error.
- You are passing the full data object to
-
- User
make this function to work for type string and number. use localecompare only for string type. function sortUIData(property, dir, type) { UI_DATA.sort((a, b) => { if (dir === 'asc') { return a[property].localeCompare(b[property], undefined, { numeric: true }); } else { return b[property].localeCompare(a[property], undefined, { numeric: true }); } }); }
- Assistant
To modify the
sortUIDatafunction to handle both string and number types appropriately, you can implement type checking within the sorting logic. UselocaleComparefor strings and direct comparison for numbers.Here's how you can do it:
function sortUIData(property, dir, type) { UI_DATA.sort((a, b) => { if (type === 'string') { if (dir === 'asc') { return a[property].localeCompare(b[property], undefined, { numeric: true }); } else { return b[property].localeCompare(a[property], undefined, { numeric: true }); } } else if (type === 'number') { if (dir === 'asc') { return a[property] - b[property]; } else { return b[property] - a[property]; } } else { console.warn(`Unsupported type: ${type}`); return 0; // No sorting if type is unsupported } }); }