JavaScript算法 – 比较两个字符串参数

huangapple go评论86阅读模式
英文:

JavaScript algorithm - Comparing two strings parameters

问题

我正在进行这个算法练习,我理解一切,除了这个条件语句的目的:

if (magazineObj[word] < 0) noteIsPossible = false

我找不到任何情况下它会产生-1的结果。这个条件语句的目的是什么?

这个算法只是返回第二个参数是否包含第一个参数中的所有单词。我展示的这段代码是教练的解决方案。

例如:

在上面的情况下,它返回true。参数2包含参数1中的所有单词。

在上面的情况下,它返回false。参数2不包含参数1中的所有单词。缺少'ee'。

在上面的情况下,它返回false。参数2不包含参数1中的所有单词。还缺少另一个'abc'。参数2必须包含2个'abc'才能返回true。

英文:

I am doing this algorithm exercise and I understand everything except the purpose of this conditional:

if (magazineObj[word] < 0) noteIsPossible = false

I can't find any scenario where this results -1

What is the purpose of this conditional?

This algorithm simply returns if the second parameter has all the words in the first parameter. This code I'm showing is the instructor's solution.

console.log(solution('aa abc dd', 'aa abc dd aa'))

function solution(noteText, magazineText) {
  var noteArr = noteText.split(' ')
  var magazineArr = magazineText.split(' ')
  var magazineObj = {}


  magazineArr.forEach(word => {
    if (!magazineObj[word]) magazineObj[word] = 0
    magazineObj[word]++
  })


  var noteIsPossible = true
  noteArr.forEach(word => {
    if (magazineObj[word]) {
      magazineObj[word]--
      if (magazineObj[word] < 0) noteIsPossible = false
    }
    else noteIsPossible = false
  })

  return noteIsPossible      
}

For example:

console.log(solution('aa abc dd', 'aa abc dd aa'))

In this case above it returns true. The parameter2 contains all words in parameter1.

 console.log(solution('aa abc dd ee', 'aa abc dd aa'))

In this case above it returns false. The parameter2 doesn't contain all words in parameter1. There is missing 'ee'

console.log(solution('aa abc abc dd', 'aa abc dd'))

In this case above it returns false. The parameter2 doesn't contain all words in parameter1. There is missing the another 'abc'. it must contain 2 'abc' in the parameter2 to return true

答案1

得分: 1

这个函数计算第一个字符串中每个“单词”出现的次数,结果是一个对象,格式如下:{ aa: 1, abc: 1, dd: 1 }

然后,代码会遍历第二个字符串,如果一个单词存在于对象中,它会从计数中减去1。如果计数小于0,那么意味着第二个字符串中该单词的实例比第一个字符串中多。

例如:'aa abc dd aa' 应该产生 - { aa: -1, abc: 0, dd: 0 },并且总体结果将是 false

然而,这实际上不起作用是因为这个条件 if (magazineObj[word])。当 magazineObj[word] 为 0 或 undefined 时,它会被评估为 false,因此永远不会达到 -1。

英文:

This function counts the number of time each "word" appears in the 1st string, and the result is an object like this { aa: 1, abc: 1, dd: 1 }.

The code then iterate the 2nd string, and if a word exists in the object it removes 1 from the number. If the counter is below 0, then it means that there are more instances of the word in the 2nd string than in the 1st string.

Example: 'aa abc dd aa' should produce - { aa: -1, abc: 0, dd: 0 }, and the overall result would be false.

However, this doesn't actually works because of this condition if (magazineObj[word]). When magazineObj[word] is 0 or undefined this will be evaluated as false, and it will never reach -1.

huangapple
  • 本文由 发表于 2020年1月3日 15:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定