1. function diffDatasets(dataset1, dataset2) {
  2. const diff = [];
  3. // Check if datasets are valid arrays
  4. if (!Array.isArray(dataset1) || !Array.isArray(dataset2)) {
  5. return "Error: Both inputs must be arrays.";
  6. }
  7. // Find the shorter length
  8. const minLength = Math.min(dataset1.length, dataset2.length);
  9. // Iterate through the datasets up to the shortest length
  10. for (let i = 0; i < minLength; i++) {
  11. if (dataset1[i] !== dataset2[i]) {
  12. diff.push({
  13. index: i,
  14. dataset1Value: dataset1[i],
  15. dataset2Value: dataset2[i],
  16. error: "Values at index " + i + " differ."
  17. });
  18. }
  19. }
  20. // Check for missing elements in dataset2
  21. if (dataset1.length > dataset2.length) {
  22. for (let i = minLength; i < dataset1.length; i++) {
  23. diff.push({
  24. index: i,
  25. dataset1Value: dataset1[i],
  26. dataset2Value: undefined,
  27. error: "Value missing in dataset2 at index " + i
  28. });
  29. }
  30. }
  31. // Check for missing elements in dataset1
  32. if (dataset2.length > dataset1.length) {
  33. for (let i = minLength; i < dataset2.length; i++) {
  34. diff.push({
  35. index: i,
  36. dataset1Value: undefined,
  37. dataset2Value: dataset2[i],
  38. error: "Value missing in dataset1 at index " + i
  39. });
  40. }
  41. }
  42. // Return the differences
  43. if (diff.length === 0) {
  44. return "Datasets are identical.";
  45. } else {
  46. return diff;
  47. }
  48. }

Add your comment