Create a JavaScript function that takes two arrays as input, one containing names of people and another containing their ages. The function should return an object with keys being the names and values being the corresponding ages. If there are duplicate names, append a number to make them unique.
PLAINFORMAT
function createObject(names, ages) {
  let result = {};

  // Iterate through the names array
  for (let i = 0; i < names.length; i++) {
    let name = names[i];
    let age = ages[i];

    // Check if the name already exists in the result object
    if (result.hasOwnProperty(name)) {
      let counter = 1;
      // Find a unique name by appending a number
      while (result.hasOwnProperty(`${name}${counter}`)) {
        counter++;
      }
      name += counter;
    }

    // Add the unique name and corresponding age to the result object
    result[name] = age;
  }

  return result;
}