# Convert Number to Words

**URL:** <https://community.weweb.io/t/convert-number-to-words/3843>\
**Category:** Ask us anything\
**Created:** [August 11, 2023, 1:01am UTC](https://community.weweb.io/t/convert-number-to-words/3843 "2023-08-11T01:01:06Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorian/32/13183_2.png) [@Dorian](https://community.weweb.io/u/Dorian)\
**Post date:** [August 11, 2023, 10:40pm UTC](https://community.weweb.io/t/convert-number-to-words/3843/6 "2023-08-11T22:40:54Z")

</div>

Thanks everyone. The switch() formula will be useful in other scenarios, but I think will be a bit too cumbersome here. Thanks @Joyce and @Alexis.

I tried using co-pilot but it rarely works anymore (see [this thread](https://community.weweb.io/t/co-pilot-has-stopped-working/3824), bug logged).

I tried using ChatGPT, but I can’t figure out how to properly reference the variable value that I want to convert from a number to text. ChatGPT gave me the below, but as it is WeWeb is returning ‘undefined’. What do I need ot change for this to run in WeWeb? Thanks.

```auto
const numberToWords = (num) => {
    const ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    const teens = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
    const tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
    
    if (num === 0) {
        return 'zero';
    }
    
    if (num < 10) {
        return ones[num - 1];
    }
    
    if (num >= 11 && num <= 19) {
        return teens[num - 11];
    }
    
    if (num % 10 === 0 && num < 100) {
        return tens[Math.floor(num / 10) - 1];
    }
    
    if (num >= 20 && num < 100) {
        return `${tens[Math.floor(num / 10) - 1]} ${ones[num % 10 - 1]}`;
    }
    
    return 'Number out of range';
};

const inputNumber = 23; // Replace with your desired number
console.log(`${inputNumber} in words is: ${numberToWords(inputNumber)}`);

```

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/5/57705d97592e578fe2e811e7f1d099bfc01a33db.png)

---

_[View the full topic](https://community.weweb.io/t/convert-number-to-words/3843)._
