Based on the following calculate function /** * * @param {Array} questionnaireItems *…
- User
Based on the following calculate function /** *
- @param {Array} questionnaireItems
- @returns {void} */ export default function (questionnaireItems) { questionnaireItems.forEach((questionnaireItem) => { let product = 1; let count = 0; let min = Number.POSITIVE_INFINITY; let max = Number.NEGATIVE_INFINITY; for (const quotation of questionnaireItem.quotations) { for (const quote of quotation.cleanedQuotes) { const price = parseFloat(quote.price); quote.price = price; if (price > 0) { product *= price; count++; min = Math.min(min, price); max = Math.max(max, price); } } } const geomean = count > 0 ? Math.pow(product, 1 / count) : 0;
const stdev = Math.sqrt( questionnaireItem.quotations.reduce((sum, quotation) => { for (const quote of quotation.cleanedQuotes) { const price = parseFloat(quote.price); if (price > 0) sum += Math.pow(price - geomean, 2); } return sum; }, 0) / count ); const variation = stdev / geomean;
if (geomean > 0) { questionnaireItem.geomean = geomean.toFixed(2); questionnaireItem.stdev = stdev.toFixed(2); questionnaireItem.variation = variation.toFixed(2); questionnaireItem.min = min.toFixed(2); questionnaireItem.max = max.toFixed(2); questionnaireItem.product = product.toFixed(2); questionnaireItem.count = count.toFixed(2); } }); }
export function recalculateRow(rowData) { // console.log("Row date is: ", rowData); let product = 1; let count = 0; let min = Number.POSITIVE_INFINITY; let max = Number.NEGATIVE_INFINITY; for (const quotation of Object.values(rowData.quotes)) { for (const key of Object.keys(quotation)) { const price = parseFloat(quotation[key]); if (isOnlyQuote(key) && price > 0) { product *= price; count++; min = Math.min(min, parseFloat(price)); max = Math.max(max, price); } } } const geomean = count > 0 ? Math.pow(product, 1 / count) : 0;
const stdev = Math.sqrt( Object.values(rowData.quotes).reduce((sum, quotation) => { for (const key of Object.keys(quotation)) { const price = parseFloat(quotation[key]); if (isOnlyQuote(key) && price > 0) sum += Math.pow(price - geomean, 2); } return sum; }, 0) / (count - 1) ); const variation = stdev / geomean;
if (geomean > 0) { rowData.geomean = isNaN(geomean) ? “0” : geomean.toFixed(2); rowData.deviation = isNaN(stdev) ? “0” : stdev.toFixed(2); rowData.variation = isNaN(variation) ? “0” : variation.toFixed(2); rowData.minQ = isNaN(min) ? “0” : min.toFixed(2); rowData.maxQ = isNaN(max) ? “0” : max.toFixed(2); rowData.product = isNaN(product) ? “0” : product.toFixed(2); rowData.count = isNaN(count) ? “0” : count.toFixed(2); rowData.ratio = isNaN(max / min) ? “0” : (max / min).toFixed(2); } // console.log("Recalculated data: ", geomean, stdev, variation, rowData); }
const isOnlyQuote = (str) => { return /^((?!_)[A-Za-z0-9])+/.test(str); }; why is the below fucntion not calculating after it fetches questionnaireItems from the backend emisAxiosInstance .get(reqUrl, { headers: { Authorization:
Bearer{auth.accessToken}}, // signal: controller.signal }) .then((response) => { // console.log('grid data response: ', response); // console.log('auth: ', auth); // Calculate geomean and stuff const { marketplaces, maxQuotation } = getMarketplacesAndMaxQuotation(response); geomean_calculator(response.data.data.questionnaireItems); - Assistant
The code snippet provided is incomplete and it is difficult to determine the exact issue without looking at the entire code. However, based on the provided code, it seems that the issue might be related to the function
geomean_calculator(). It is unclear what this function does and how it processes thequestionnaireItemsarray.Additionally, it is important to make sure that the
response.data.data.questionnaireItemsarray is not null or undefined before passing it to thegeomean_calculator()function. This can be checked by adding a null/undefined check like this:if (response.data.data.questionnaireItems) { geomean_calculator(response.data.data.questionnaireItems); }It is also important to check if the
setReqResponseData(),generateRows(), andgenerateColumns()functions are working as expected and not throwing any errors. - User
questionnaireItems are being fetched from the backend
- Assistant
Yes, I understand that the
questionnaireItemsare being fetched from the backend, but it is still important to check if the fetched data is not null or undefined before processing it further. It is possible that the backend may not return any data or the data may be in an unexpected format which can cause errors in thegeomean_calculator()function. Therefore, it is always a good practice to add null/undefined checks before using any data fetched from an external source. - User
even with the above corrections its still calculating wrong
- Assistant
If the function is still calculating the wrong value after the suggested corrections, it is possible that the issue lies within the logic of the
geomean_calculator()function itself. Here are some potential areas to investigate:-
Check if the calculation of the
productvariable is correct. Theproductvariable is being multiplied by the price of each quote, but it only needs to include prices greater than 0. It is also possible that the product is overflowing the range of floating point arithmetic, causing the result to be incorrect. -
Verify that the calculation of the
geomeanvariable is correct. Thegeomeanis calculated as the nth root of the product of all quotes, where n is the number of quotes. Ensure that the root is being calculated correctly and the result is being rounded to the correct number of decimal places. -
Check the calculation of the and variables. Ensure that the square root and division operations are correct and the results are being rounded to the correct number of decimal places.
-
- User
const mutateRows = (newItem, p) => { let pos; const data = p.data; const quote = p.colDef.field; const marketplaceCode = p.column.originalParent.colGroupDef.field; const newValue = parseFloat(p.newValue);
rowImmutableStore = rowImmutableStore.map((oldItem, index) => { if (oldItem.id === newItem.id) { pos = index; return newItem; } return oldItem; }); // console.log('onCellEditRequest, updating ' + quote + ' to ' + newValue, pos); // console.log("rowImmutableStore: ", pos, rowImmutableStore); if (pos !== "undefined") recalculateRow(rowImmutableStore[pos]); setRowData(rowImmutableStore); // console.log("CHanges: ", event); setChanges((state) => [ ...state, { quote, marketplace: marketplaceCode.toString(), oldValue: typeof p.oldValue === "number" ? parseFloat(p.oldValue) : undefined, newValue, itemCode: data.code.toString(), row: p.rowIndex, col: p.column.getInstanceId(), geomean: data.geomean, variation: data.variation, deviation: data.deviation, count: data.count, maxQ: data.maxQ, minQ: data.minQ, ratio: data.ratio, }, ]);}; is mutateRows correct with respect to the above functions. This is where i update a row when a value is changed
- Assistant
Based on the provided code, it seems that the
mutateRows()function is correct and is being called correctly when a cell value is edited. The function updates therowImmutableStorearray with the updated row and then recalculates the geomean and other statistical values using therecalculateRow()function.After the recalculation, the
setRowData()function updates the table with the updated data and thesetChanges()function adds an entry to thechangesarray with the updated values.However, it is important to note that the
rowImmutableStorearray is being updated directly, which can cause unexpected behavior and make it difficult to track changes in the data. It may be better to make a copy of the array using theslice()function before updating it to ensure immutability. For example:rowImmutableStore = rowImmutableStore.slice(); rowImmutableStore[pos] = newItem;This will create a shallow copy of the
rowImmutableStorearray and then update the copy with the new values. This ensures that the original array remains immutable and any changes can be tracked appropriately. - User
where do i make the above change
- Assistant
You can make the change in the
mutateRows()function, before updating therowImmutableStorearray. Here's an updated version of themutateRows()function with the change:const mutateRows = (newItem, p) => { let pos; const data = p.data; const quote = p.colDef.field; const marketplaceCode = p.column.originalParent.colGroupDef.field; const newValue = parseFloat(p.newValue); // ----- Make a copy of rowImmutableStore ----- const newImmutableStore = rowImmutableStore.slice(); newImmutableStore.forEach((oldItem, index) => { if (oldItem.id === newItem.id) { pos = index; return newItem; } }); // console.log('onCellEditRequest, updating ' + quote + ' to ' + newValue, pos); // console.log("rowImmutableStore: ", pos, newImmutableStore); if (pos !== "undefined") recalculateRow(newImmutableStore[pos]); setRowData(newImmutableStore); // console.log("CHanges: ", event); setChanges((state) => [ ...state, { quote, marketplace: marketplaceCode.toString(), oldValue: typeof p.oldValue === "number" ? parseFloat(p.oldValue) : undefined, newValue, itemCode: data.code.toString(), row: p.rowIndex, col: p.column.getInstanceId(), geomean: data.geomean, variation: data.variation, deviation: data.deviation, count: data.count, maxQ: data.maxQ, minQ: data.minQ, ratio: data.ratio, }, ]); };