Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new challenge added: To convert a entered number to its numeric word … #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Challenges/NumbersToWords/NumbersToWords.module.css
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions src/Challenges/NumbersToWords/constants.js
Original file line number Diff line number Diff line change
@@ -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'];
75 changes: 75 additions & 0 deletions src/Challenges/NumbersToWords/index.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.container}>
<h1 className={styles.center}>Number To Words</h1>
<div className={styles.inputContainer}>
<div id="numberInput">Enter the number</div>
<input
className={styles.numberInput}
aria-labelledby="numberInput"
type="number"
value={number}
onChange={(e) => setNumber(e.target.value)}
/>

{/* onSubmit convert the number to words and display below */}
</div>
<div className={styles.center}>
<button
type="button"
className={styles.submitBtn}
onClick={convertNumbersToWords}
>
Submit
</button>
</div>

<h3 className={`${styles.center} ${styles.word}`}>In words: {word}</h3>

</div>
)
}

export default NumbersToWords;
26 changes: 26 additions & 0 deletions src/Challenges/NumbersToWords/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ones, tens, teens } from "./constants"

// Function to convert numbers less than 1000 into words
export const convertLessThanOneThousand = (num) => {
let result = '';

if (num >= 100) {
result += ones[Math.floor(num / 100)] + ' hundred ';
num %= 100;
}

if (num >= 20) {
result += tens[Math.floor(num / 10)] + ' ';
num %= 10;
}

if (num > 0) {
if (num < 10) {
result += ones[num] + ' ';
} else {
result += teens[num - 10] + ' ';
}
}

return result.trim();
}
7 changes: 7 additions & 0 deletions src/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SnakeLadderBoard from "./Challenges/SnakeLadder/SnakeLadderBoard";
import StarRating from "./Challenges/StarRating/StarRating";
import TicTacToe from "./Challenges/TicTacToe/TicTacToe";
import NestedDropDown from "./Challenges/NestedDropDown/NestedDropDown.jsx"
import NumbersToWords from "./Challenges/NumbersToWords";

const basePath = "https://github.com/adavijit/frontend-challenges/tree/main/src/Challenges";
const challenges = [
Expand Down Expand Up @@ -51,6 +52,12 @@ const challenges = [
"source": `${basePath}/AutoComplete`,
"component": <AutoComplete />
},
{
"name": "Numbers To words",
"contributors": ["rupali-yadav"],
"source": `${basePath}/AutoComplete`,
"component": <NumbersToWords />
}
];

export default challenges;