diff --git a/src/Challenges/NumbersToWords/NumbersToWords.module.css b/src/Challenges/NumbersToWords/NumbersToWords.module.css new file mode 100644 index 0000000..fdfd220 --- /dev/null +++ b/src/Challenges/NumbersToWords/NumbersToWords.module.css @@ -0,0 +1,35 @@ +.container { + padding: 0 0 80px; + font-size: 16px; +} + +.inputContainer { + display: flex; + align-items: center; + justify-content: center; + padding: 20px; +} + +.center { + margin: 0 auto; + text-align: center; +} + +.numberInput { + /* border: none; */ + /* border-bottom: 1px solid #000; */ + font-size: 18px; + padding: 5px 10px; + text-align: center; + width: 100%; +} + +.submitBtn { + padding: 10px 30px; + border-radius: 4px; + font-size: 16px; +} + +.word { + padding: 30px 10px; +} \ No newline at end of file diff --git a/src/Challenges/NumbersToWords/constants.js b/src/Challenges/NumbersToWords/constants.js new file mode 100644 index 0000000..0dd6040 --- /dev/null +++ b/src/Challenges/NumbersToWords/constants.js @@ -0,0 +1,3 @@ +export const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; +export const teens = ['', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']; +export const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; diff --git a/src/Challenges/NumbersToWords/index.jsx b/src/Challenges/NumbersToWords/index.jsx new file mode 100644 index 0000000..3ddad22 --- /dev/null +++ b/src/Challenges/NumbersToWords/index.jsx @@ -0,0 +1,75 @@ +import { useState } from "react"; +import { convertLessThanOneThousand } from "./utils"; +import styles from "./NumbersToWords.module.css"; + +const NumbersToWords = () => { + + const [word, setWord] = useState(''); + const [number, setNumber] = useState(''); + + const convertNumbersToWords = () => { + let num = number; + if (num === 0) { + return 'zero'; + } + + let result = ''; + + if (num < 0) { + result = 'negative '; + num = Math.abs(num); + } + + if (num >= 10000000) { + result += convertLessThanOneThousand(Math.floor(num / 10000000)) + ' crore '; + num %= 10000000; + } + + if (num >= 100000) { + result += convertLessThanOneThousand(Math.floor(num / 100000)) + ' lakh '; + num %= 100000; + } + + if (num >= 1000) { + result += convertLessThanOneThousand(Math.floor(num / 1000)) + ' thousand '; + num %= 1000; + } + + result += convertLessThanOneThousand(num); + + setWord(result.trim()); + } + + + return ( +