Open softmuneeb opened 1 year ago
const axios = require('axios'); const nanoid = require('nanoid');
async function getOwnerName(phoneNumber) {
try {
const response = await axios.get(https://api.telnyx.com/v2/phone_number/lookup/${phoneNumber}
, {
headers: {
'Authorization': 'Bearer
if (response.status === 200) {
return response.data.owner_name;
} else {
return null;
}
} catch (error) { return null; } }
async function generatePhoneNumbers(prefixes, count) { const phoneNumbers = [];
for (let i = 0; i < count; i++) { const prefix = prefixes[Math.floor(Math.random() * prefixes.length)]; const phoneNumber = prefix + nanoid(9); // Generate a random 9-digit number
const ownerName = await getOwnerName(phoneNumber);
if (ownerName) {
phoneNumbers.push({ phoneNumber, ownerName });
}
}
return phoneNumbers; }
generatePhoneNumbers(['+1212', '+1315'], 3) .then(phoneNumbers => { console.log('(3 phone numbers with names)'); phoneNumbers.forEach(entry => { console.log(entry.phoneNumber, entry.ownerName); }); }) .catch(error => { console.error('Error:', error); });
Good Try! but we need output like this: +12125483261 John David +12128875485 Lucy Williams +12128732177 Joe Millions
If you want the format you specified above, the code block should be as follows:
generatePhoneNumbers(['+1212', '+1315'], 3) .then(phoneNumbers => { phoneNumbers.forEach(entry => { console.log(entry); }); }) .catch(error => { console.error('Error:', error); });
const axios = require('axios'); const nanoid = require('nanoid');
async function getOwnerName(phoneNumber) {
try {
const response = await axios.get(https://api.telnyx.com/v2/phone_number/lookup/${phoneNumber}
, {
headers: {
'Authorization': 'Bearer
if (response.status === 200) {
return response.data.owner_name;
} else {
return null;
}
} catch (error) { return null; } }
async function generatePhoneNumbers(prefixes, count) { const phoneNumbers = [];
for (let i = 0; i < count; i++) { const prefix = prefixes[Math.floor(Math.random() * prefixes.length)]; const phoneNumber = prefix + nanoid(9); // Generate a random 9-digit number
const ownerName = await getOwnerName(phoneNumber);
if (ownerName) {
phoneNumbers.push(`${phoneNumber} ${ownerName}`);
} else {
i--; // Retry if owner name is not found
}
}
return phoneNumbers; }
generatePhoneNumbers(['+1212', '+1315'], 3) .then(phoneNumbers => { phoneNumbers.forEach(entry => { console.log(entry); }); }) .catch(error => { console.error('Error:', error); });
Code JavaScript (node.js) for: Input: generatePhoneNumbers([“+1212”, “+1315”], 3)
Output: (3 phone numbers with names) +12125483261 John David +12128875485 Lucy Williams +12128732177 Joe Millions Explanation: print 3 valid phone numbers with owner names starting from “+1212” or “+1315” or … Apis to use: axios, nanoid, telnyx, amazon pinpoint
P.S: Can use chatgpt or any tool Submit code here