Home > OS >  How to generate random names when given two arrays of objects with key value pairs
How to generate random names when given two arrays of objects with key value pairs

Time:01-06

I'm trying to solve an algorithmic problem from codewars.

Given a function aliasGen(){} with an object { '0': 'Mike', '1': 'Millington' } as an argument, I need to extract the first letter of each value and match them to other data objects firstName = [{ A: 'Alpha', ...etc] && surname=[{ A: 'Analogue', B: 'Bomb',C: 'Catalyst', ...etc}] to generate a random alias.

firstname and surname are given and contain the "random names" to match from

var firstName = { A: 'Alpha', B: 'Beta', C: 'Cache', D: 'Data', E: 'Energy', ...etc}
var surname = { A: 'Analogue', B: 'Bomb', C: 'Catalyst', D: 'Discharge', E: 'Electron', ...etc}

Some of the test cases

Test.assertEquals(aliasGen("Mike", "Millington"), "Malware Mike");
Test.assertEquals(aliasGen("Fahima", "Tash"), "Function T-Rex");
Test.assertEquals(aliasGen("Daisy", "Petrovic"), "Data Payload");

Special Cases

  1. If the first character of either of the names given to the function is not a letter from A - Z, you should return "Your name must start with a letter from A - Z."

  2. Sometimes people might forget to capitalize the first letter of their name so your function should accommodate for these grammatical errors.

This is the kata just in case: codewars

I actually think I'm over engineering this. Right now I'm actually stuck on the Special Cases.

This is what I have now.

function aliasGen(){
    // Code Here
  // console.log(firstName)
  // console.log(surname)
  
  console.log(arguments) // { '0': 'Mike', '1': 'Millington' }

  let  propertyNames  = Object.values(arguments)
  console.log(propertyNames) // [ 'Mike', 'Millington' ]

  var firstLetters = propertyNames.map(name => name[0])
  
  console.log(firstLetters, 'firstLetters') [ 'M', 'M' ] 'firstLetters'
  
  let lettersRegexp = /[A-Z]/;
  
  if (firstLetters.map( letter => lettersRegexp.test(letter))) {
    
    var firstNamePseudoObj = Object.keys(firstName)
    .filter((key) => key.includes(firstLetters[0]))
    .reduce((obj, key) => {
        return Object.assign(obj, {
          [key]: firstName[key]
        });
  }, {});
  
  var lastNamePseudoObj = Object.keys(surname)
    .filter((key) => key.includes(firstLetters[1]))
    .reduce((obj, key) => {
        return Object.assign(obj, {
          [key]: surname[key]
        });
  }, {});
  
  let arrayOfPseudos = [Object.values(firstNamePseudoObj), Object.values(lastNamePseudoObj)].map(arr => String(arr)).join(' ')
  
  console.log(Object.values(firstNamePseudoObj), 'firstPseudo') // [ 'Malware' ] 'firstPseudo'

  console.log(Object.values(lastNamePseudoObj), 'lastPseudo') [ 'Mike' ] 'lastPseudo'

  
  console.log(arrayOfPseudos) Malware Mike

  
  return arrayOfPseudos
     
  }  else {
        return `Your name must start with a letter from A - Z.`
  }
}

I fail the following test cases

Log
{ '0': 'Anuddanumbha', '1': '23200' }
[ 'Anuddanumbha', '23200' ]
[ 'A', '2' ] 'firstLetters'
false
[ 'Alpha' ] 'firstPseudo'
[] 'lastPseudo'
Alpha 

Expected: 'Your name must start with a letter from A - Z.', instead got: 'Alpha '

---

Log
{ '0': '82ckt', '1': 'vuvmy' }
[ '82ckt', 'vuvmy' ]
[ '8', 'v' ] 'firstLetters'
false
[] 'firstPseudo'
[] 'lastPseudo'


---

Log
{ '0': 'di5io', '1': 'tudou' }
[ 'di5io', 'tudou' ]
[ 'd', 't' ] 'firstLetters'
false
[] 'firstPseudo'
[] 'lastPseudo'
 
Expected: 'Data T-Rex', instead got: 

CodePudding user response:

You can simply take the first character of values passed to you, and then you need to check if it is an alphabet or not.

function aliasGen(first, second) {
  let firstNameChar = first[0].toUpperCase()
  let lastNameChar = second[0].toUpperCase()

  if (/[a-z]/i.test(firstNameChar) && /[a-z]/i.test(lastNameChar)) {
    return firstName[firstNameChar]   " "   surname[lastNameChar]
  } else {
    return "Your name must start with a letter from A - Z."
  }
}
  • Related