function diffDatasets(dataset1, dataset2) {
const diff = [];
// Check if datasets are valid arrays
if (!Array.isArray(dataset1) || !Array.isArray(dataset2)) {
return "Error: Both inputs must be arrays.";
}
// Find the shorter length
const minLength = Math.min(dataset1.length, dataset2.length);
// Iterate through the datasets up to the shortest length
for (let i = 0; i < minLength; i++) {
if (dataset1[i] !== dataset2[i]) {
diff.push({
index: i,
dataset1Value: dataset1[i],
dataset2Value: dataset2[i],
error: "Values at index " + i + " differ."
});
}
}
// Check for missing elements in dataset2
if (dataset1.length > dataset2.length) {
for (let i = minLength; i < dataset1.length; i++) {
diff.push({
index: i,
dataset1Value: dataset1[i],
dataset2Value: undefined,
error: "Value missing in dataset2 at index " + i
});
}
}
// Check for missing elements in dataset1
if (dataset2.length > dataset1.length) {
for (let i = minLength; i < dataset2.length; i++) {
diff.push({
index: i,
dataset1Value: undefined,
dataset2Value: dataset2[i],
error: "Value missing in dataset1 at index " + i
});
}
}
// Return the differences
if (diff.length === 0) {
return "Datasets are identical.";
} else {
return diff;
}
}
Add your comment