Spot the bug - #118: Name Badge Generator

My cute wizard title formatter keeps printing undefined words.

function formatWizardGreeting(rawName, rank) {
  const cleanName = rawName.trim();
  const safeRank = rank.toLowerCase();

  let prefix = '';
  if (safeRank === 'archmage') {
    prefix = 'The Grand';
  } else if (safeRank === 'apprentice') {
    prefix = 'Novice';
  } else {
    prefix = 'Traveler';
  }

  // Capitalize first letter of wizard name
  const firstChar = cleanName.charAt(0).toUpperCase();
  const restOfName = cleanName.subString(1).toLowerCase();
  const styledName = firstChar + restOfName;

  const magicBanner = `*~ ${prefix} ${styledName} ~*`;
  return magicBanner.repeat(1);
}

const greeting = formatWizardGreeting('  gANDALF ', 'archmage');
console.log(greeting);

Reply with what is broken and how you would fix it.