Currently, I am in the process of expanding my knowledge of JavaScript. As I delve into challenges on Codewars, it has become evident that there are gaps in my understanding. One particular challenge I faced involved formatting a phone number using a 'for loop' to push characters into an array. Despite numerous attempts at different solutions, I have yet to achieve the desired outcome. Any guidance on identifying errors in my approach and addressing the flaws in my logic would be greatly appreciated. Below is my best attempt so far:
const createPhoneNumber = (phoneNumber) => {
let formattedNumber = [];
formattedNumber.push(phoneNumber)
for (let i = 0; i < formattedNumber.length; i++) {
if (formattedNumber[i] === 0) {
formattedNumber.push('(')
}
if (formattedNumber[i] === 2) {
formattedNumber.push(')')
}
if (formattedNumber[i] === 5) {
formattedNumber.push('-')
}
}
return(formattedNumber.toString());
}
console.log(createPhoneNumber(1234567890));