CodeRefine

Code Input

JavaScript
42 lines

Code Analysis

3 issues

Potential Performance Issue

Array length accessed in loop condition - store in variable

Line 2, 9

Redundant Code

Similar loop patterns in calculateSum and findMax

Lines 1-7, 8-14

Error Handling

No validation for empty array in processData

Line 15
Code Quality Score 72%

Refactor Code

Ready

Refactoring Options

Estimated time: 3 seconds

Refactored Code Preview

// Refactored code with improvements
function calculateSum(arr) {
  if (!arr.length) return 0;
  return arr.reduce((total, num) => total + num, 0);
}

function findMax(arr) {
  if (!arr.length) return -Infinity;
  return Math.max(...arr);
}

function processData(data) {
  if (!data || !data.length) {
    console.warn('Empty or undefined data provided');
    return {};
  }
  
  const sum = calculateSum(data);
  const max = findMax(data);
  const average = sum / data.length;
  
  console.log('Sum:', sum);
  console.log('Max:', max);
  console.log('Average:', average);
  
  return { sum, max, average };
}

// Example usage
const sampleData = [12, 45, 67, 23, 89, 34, 56];
processData(sampleData);