Sobes.tech
Middle+

// Les données du serveur concernant le prix du produit sont reçues sous la forme: // 12345678 (le prix peut être n'importe quel nombre dans la plage de 1 à [phone]) // il est nécessaire d'écrire une fonction qui convertira le prix en une chaîne formatée comme suit: // 12'345'678

sobes.tech IA

Réponse de l'IA

Для форматирования числа с разделением тысяч апострофами можно использовать регулярное выражение или метод toLocaleString с кастомным локалем. Пример с регулярным выражением:

function formatPrice(price) {
  return price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "'");
}

console.log(formatPrice(12345678)); // "12'345'678"

Здесь \B — позиция не на границе слова, (?=(\d{3})+(?!\d)) — смотрит вперед на группы по 3 цифры, не доходя до конца строки, и вставляет апострофы между ними.