Skip to content

Commit

Permalink
Merge pull request #2489 from Saipradyumnagoud/master
Browse files Browse the repository at this point in the history
Added Letter Combination of a phone number Game
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 22, 2024
2 parents b8eee56 + 483271e commit c2c6364
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Phone Number Letter Combination/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Phone Number Letter Combination</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Phone Number Letter Combination</h1>
<input type="text" id="phone-number" placeholder="Enter phone number" maxlength="10">
<button onclick="generateCombinations()">Generate Combinations</button>
<div id="combinations"></div>
</div>
<script src="script.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions Phone Number Letter Combination/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Phone Number Letter Combination",
"short_name": "LetterCombos",
"description": "A web application that generates all possible letter combinations for a given phone number based on a traditional phone keypad.",
"start_url": "/",
"display": "standalone",
"background_color": "#f0f0f0",
"theme_color": "#4caf50",
"icons": [
{
"src": "icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}

50 changes: 50 additions & 0 deletions Phone Number Letter Combination/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const digitToLetters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
};

function generateCombinations() {
const phoneNumber = document.getElementById('phone-number').value;
const resultContainer = document.getElementById('combinations');
resultContainer.innerHTML = '';

if (!/^[2-9]+$/.test(phoneNumber)) {
resultContainer.innerHTML = '<p>Please enter a valid phone number containing digits 2-9.</p>';
return;
}

const combinations = getCombinations(phoneNumber);

combinations.forEach(combination => {
const p = document.createElement('p');
p.textContent = combination;
resultContainer.appendChild(p);
});
}

function getCombinations(digits) {
if (digits.length === 0) {
return [];
}
if (digits.length === 1) {
return digitToLetters[digits[0]].split('');
}

const prevCombinations = getCombinations(digits.slice(0, -1));
const additionalLetters = digitToLetters[digits[digits.length - 1]];
const newCombinations = [];

prevCombinations.forEach(combination => {
for (const letter of additionalLetters) {
newCombinations.push(combination + letter);
}
});

return newCombinations;
}
57 changes: 57 additions & 0 deletions Phone Number Letter Combination/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

.container {
text-align: center;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
margin-bottom: 20px;
}

input {
padding: 10px;
font-size: 16px;
margin-bottom: 20px;
width: 80%;
max-width: 300px;
border: 1px solid #ccc;
border-radius: 4px;
}

button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
background-color: #4caf50;
color: white;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

#combinations {
margin-top: 20px;
text-align: left;
max-height: 300px;
overflow-y: auto;
}

#combinations p {
padding: 5px;
border-bottom: 1px solid #ddd;
}

0 comments on commit c2c6364

Please sign in to comment.