【问题标题】:String Match challenge in JavascriptJavascript中的字符串匹配挑战
【发布时间】:2021-12-29 18:12:38
【问题描述】:

如果有人能指出或只是提供线索我做错了什么,将不胜感激。所以任务是:

给定 2 个字符串,a 和 b,返回位置的数量 它们包含相同的长度为 2 的子字符串。所以“xxcaazz”和“xxbaaz” 产生 3,因为“xx”、“xx”、“aa”和“az”子字符串出现在 两个字符串中的相同位置。

function('xxcaazz', 'xxbaaz') 应该返回 3

function('abc', 'abc') 应该返回 2

function('abc', 'axc') 应该返回 0

我的代码:

function stringMatch(a, b){

//  convert both strings to arrays with split method

  let arrA = a.split("")
  let arrB = b.split("")

//  create 2 empty arrays to feel in with symbol combinations

  let arrOne = [];
  let arrTwo = [];

// loop through the first array arrA and push elements to empty arrayOne

  for ( let i = 0; i < arrA.length ; i++ ) {
      arrOne.push(arrA[i]+arrA[i+1])
  }
// loop through the first array arrB and push elements to empty arrayTwo

  for ( let i = 0; i < arrB.length ; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }
//  create a new array of the matching elements from arrOne and arrTwo

  let newArray = arrOne.filter(value => arrTwo.includes(value))

//   return the length 0f the newArray - that's supposed to be the answer

  return newArray.length
}

感谢您的帮助!

【问题讨论】:

  • How do I ask a good question?: "描述问题。“它不起作用”的描述不足以帮助人们理解你的问题。相反,告诉其他人读者预期的行为应该是什么。告诉其他读者错误消息的确切措辞是什么,以及产生错误消息的代码行。使用简短但描述性的问题摘要作为问题的标题。” + 添加带有输入、预期和实际输出的minimal reproducible example
  • 我的错,谢谢。
  • 在这种情况下,问题出在哪里很明显,即使不明显,复制代码并自己运行也需要 5 秒钟,但有些人更喜欢批评而不是帮助。

标签: javascript arrays string methods


【解决方案1】:

在循环的最后一次迭代中,不会有“下一个”字符,arrB[i+1] 将是未定义的。解决这个问题的最简单方法是只循环到 倒数第二个字符,或者直到i &lt; arrB.length - 1

for ( let i = 0; i < arrB.length - 1; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }

例如...

console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0

function stringMatch(a, b){

//  convert both strings to arrays with split method

  let arrA = a.split("")
  let arrB = b.split("")

//  create 2 empty arrays to feel in with symbol combinations

  let arrOne = [];
  let arrTwo = [];

// loop through the first array arrA and push elements to empty arrayOne

  for ( let i = 0; i < arrA.length -1 ; i++ ) {
      arrOne.push(arrA[i]+arrA[i+1])
  }
// loop through the first array arrB and push elements to empty arrayTwo

  for ( let i = 0; i < arrB.length - 1; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }
  
//  create a new array of the matching elements from arrOne and arrTwo

  let newArray = arrOne.filter(value => arrTwo.includes(value))

//   return the length 0f the newArray - that's supposed to be the answer

  return newArray.length
}

作为奖励,这是我自己的解决方案...

console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0

function stringMatch(a, b){
  var matches = 0;
  for(let i=a.length-1; i--;){
    let s1 = a.substring(i, i+2);
    let s2 = b.substring(i, i+2);
    if(s1 == s2) matches++;
  }
  return matches;
}

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-24
    • 2012-06-29
    • 2011-09-25
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多