softmuneeb / code-and-earn

0 stars 0 forks source link

🌟 Freelancing Opportunity: Code a JavaScript function in Node.js! Task: Generate and verify phone numbers with APIs Reward: 5000 Rs Completing this task could lead to more opportunities in the future #NodeJS #FreelancingOpportunity #1

Open softmuneeb opened 1 year ago

softmuneeb commented 1 year ago

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

usama-amir commented 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); });

softmuneeb commented 1 year ago

Good Try! but we need output like this: +12125483261 John David +12128875485 Lucy Williams +12128732177 Joe Millions

usama-amir commented 1 year ago

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); });

usama-amir commented 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}`);
} 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); });