From db99770f4edf8754484f22fb0318b409d6596aec Mon Sep 17 00:00:00 2001 From: John Serrano Date: Mon, 23 Dec 2024 12:11:12 -0500 Subject: [PATCH] Solution challenge 18 v2 --- challenges-2024/challenge-18/findInAgenda.js | 37 +++++++------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/challenges-2024/challenge-18/findInAgenda.js b/challenges-2024/challenge-18/findInAgenda.js index 9f2bf74..6d99e37 100644 --- a/challenges-2024/challenge-18/findInAgenda.js +++ b/challenges-2024/challenge-18/findInAgenda.js @@ -1,36 +1,25 @@ export function findInAgenda(agenda, phone) { - // Code here - // const regex = /\+\d[\d\-]*\d/ - - // const numero = "34-600-123-456"; // NĂºmero dinĂ¡mico const regex = new RegExp(phone.replace(/-/g, '\\-'), 'g') - const text = agenda.split('\n').find(line => regex.test(line)) - // console.log({ text }) - const matches = [...agenda.matchAll(regex)] - console.log({ matches }) - if (matches.length > 1 || !text) return null - const name = text.split('<')[1].split('>')[0] - const address = text.split('<')[0].split(' ').slice(1).join(' ').trim() + if (matches.length === 0) return null + + const results = matches.map(match => { + const text = agenda.split('\n').find(line => line.includes(match[0])) + const name = text.split('<')[1].split('>')[0] + const address = text.split('<')[0].split(' ').slice(1).join(' ').trim() + return { name, address } + }) - return { name, address } + if (results.length > 1) return null + + return results[0] } const agenda = `+34-600-123-456 Calle Gran Via 12 Plaza Mayor 45 Madrid 28013 +34-600-987-654 +1-800-555-0199 Fifth Ave New York` -// const result = findInAgenda(agenda, '34-600-123-456') -// console.log(result) - -// console.log(findInAgenda(agenda, '600-987')) - -console.log( - findInAgenda( - `+44-123-456-789 Main Street - +44-123-456-789 Park Avenue `, - '44-123-456-789' - ) -) +const result = findInAgenda(agenda, '34-600-123-456') +console.log(result)