Skip to content

Commit

Permalink
Solution challenge 18 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsi15 committed Dec 23, 2024
1 parent b9ab79f commit db99770
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions challenges-2024/challenge-18/findInAgenda.js
Original file line number Diff line number Diff line change
@@ -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 <Juan Perez>
Plaza Mayor 45 Madrid 28013 <Maria Gomez> +34-600-987-654
<Carlos Ruiz> +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 <Alice Smith>
+44-123-456-789 Park Avenue <Alice Johnson>`,
'44-123-456-789'
)
)
const result = findInAgenda(agenda, '34-600-123-456')
console.log(result)

0 comments on commit db99770

Please sign in to comment.