Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Parsing & sorting script; value reconciliation and validation

nabaati

New Coder
I am trying to make a parsing script in VSC that runs through a text file containing lines like this :

Price: €145,000
m²: 6m², 62.6m²

Price: €389,000
m²: 157m², 340m², 340m²

Price: €399,000
m²: 24m², 24m², 150.24m²

Price: €456,000
m²: 421m², 421m², 421m²

And what i am trying is make the script for me give a finalized m2 value using a logic in pattern with this:

EXAMPLES: 19m2 , 19m2 , 109,19m2 = 109,19m2
109m2 , 109m2 , 109m2 = 109m2
109,19m2 , 19m2 = 109,19m2
109,19m2 , 109,19m2 = 109,19m2
109,19m2 , 50m2 = flagged to be checked printing 'CHECK' next to m2 values
109,19m2 , 109,19m2 , 50m2 = flagged to be checked printing 'CHECK' next to m2 values
86m2, 270m², 1086m = flagged to be checked printing 'CHECK' next to m2 values


I do not know coding, I am making extensive use out of chatGPT and claudeAI.
but both have reached the limit in allowing me to move forward.

Now i am posting here in an attempt to maybe actually learn something as to why my current code is not performing the intended task.

the code:
const fs = require('fs');
// Function to determine the final m² value
function determineFinalM2(values) {
// Remove non-numeric characters and convert to float
const numericValues = values.map(value => parseFloat(value.replace(/[^\d.]/g, '')));
// Function to check if all values are the same
const allSame = arr => arr.every(v => v === arr[0]);
// Check if all values are the same
if (allSame(numericValues)) {
return values[0]; // Return the first value if all are the same
}
// Check for cases where one value is clearly a decimal part of another larger value
const mainValues = numericValues.filter(value => value >= 100);
const decimalValues = numericValues.filter(value => value < 100);
if (mainValues.length === 1 && decimalValues.length === 1) {
return ${mainValues[0]},${decimalValues[0]}m²;
}
// If there are exactly two main values and they are the same
if (mainValues.length === 2 && mainValues[0] === mainValues[1]) {
return ${mainValues[0]}m²;
}
// If there are more than one main values and they don't match, flag with "CHECK"
return CHECK ${values.join(', ')};
}
// Read contents of Dump.txt
fs.readFile('Dump.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Split data by double newline to separate listings
const listings = data.trim().split('\n\n');
// Object to store grouped listings
const groupedListings = {};
// Process each listing
listings.forEach((listing) => {
const lines = listing.split('\n');
const id = lines[0].replace('ID: ', '');
const location = lines[1].replace('Location: ', '');
const m2Values = [];
let price = '';
// Collect m² values and price
lines.forEach(line => {
if (line.includes('m²:')) {
const m2Value = line.replace('m²:', '').trim();
m2Values.push(m2Value);
} else if (line.startsWith('Price:')) {
price = line.replace('Price: ', '').trim();
}
});
// Determine the final m² value
const finalM2 = determineFinalM2(m2Values);
// Store or update grouped listings
if (!groupedListings[location]) {
groupedListings[location] = [];
}
groupedListings[location].push({
id: id,
price: price,
m2: finalM2
});
});
// Prepare output for console.txt
const consoleOutput = [];
Object.keys(groupedListings).forEach((location) => {
consoleOutput.push(Location: ${location});
groupedListings[location].forEach(listing => {
consoleOutput.push(ID: ${listing.id});
consoleOutput.push(Price: ${listing.price});
consoleOutput.push(m²: ${listing.m2});
consoleOutput.push(''); // Empty line between listings
});
});
// Write to console.txt (override previous content)
fs.writeFileSync('console.txt', consoleOutput.join('\n'));
console.log('Data has been written to console.txt');
});

two examples of the current output instead of intended one (rest of the text file in the same output):


Price: €359,000
m²: 86m2, 270m², 1086m

Price: Offer accepted
m²: 63m², 63m², 117.63m²
 
Hey there.
When positing code, please post it in the code blocks. This can be done using the </> button, BBCode or Markdown.

And what i am trying is make the script for me give a finalized m2 value using a logic in pattern with this:
You have to explain what the logic you're trying to do is. Simply giving examples is not enough for us to figure out what you're trying to do.
 

New Threads

Buy us a coffee!

Back
Top Bottom