繁体   English   中英

给定两个 arrays 具有键值对的对象时如何生成随机名称

[英]How to generate random names when given two arrays of objects with key value pairs

我正在尝试解决代码战中的算法问题。

给定一个 function aliasGen(){}和一个 object { '0': 'Mike', '1': 'Millington' }作为参数,我需要提取每个值的第一个字母并将它们与其他数据对象firstName = [{ A: 'Alpha', ...etc] && surname=[{ A: 'Analogue', B: 'Bomb',C: 'Catalyst', ...etc}]匹配firstName = [{ A: 'Alpha', ...etc] && surname=[{ A: 'Analogue', B: 'Bomb',C: 'Catalyst', ...etc}]生成随机别名。

给出firstnamesurname ,并包含要匹配的“随机名称”

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}

一些测试用例

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

特别案例

  1. 如果给 function 的任何一个名字的第一个字符不是 A-Z 中的字母,您应该返回“您的名字必须以 A-Z 中的字母开头”。

  2. 有时人们可能会忘记将他们名字的第一个字母大写,因此您的 function 应该可以解决这些语法错误。

这是以防万一的套路: codewars

我实际上认为我过度设计了这个。 现在我实际上停留在Special Cases上。

这就是我现在所拥有的。

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.`
  }
}

我没有通过以下测试用例

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: 

您可以简单地获取传递给您的值的第一个字符,然后您需要检查它是否是字母表。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM