寻求一些关于为什么我的if语句不起作用的澄清。

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

Looking for some clarification on why my if statement isn't working

问题

Here is the translated content:

故事

YouTube 有一个喜欢和不喜欢的按钮,允许用户表达他们对特定内容的意见。它被设置成这样,你不能同时喜欢和不喜欢一个视频。

还有两个有趣的界面规则需要注意:按下一个已经处于活动状态的按钮将撤消您的按下操作。如果您在按下不喜欢按钮之后按下喜欢按钮,则喜欢按钮将覆盖先前的“不喜欢”状态。反之亦然。

任务

创建一个接受按钮输入列表并返回最终状态的函数。

示例

likeOrDislike([不喜欢]) => 不喜欢
likeOrDislike([喜欢, 喜欢]) => 无
likeOrDislike([不喜欢, 喜欢]) => 喜欢
likeOrDislike([喜欢, 不喜欢, 不喜欢]) => 无

注意事项

如果没有按钮处于活动状态,请返回无。
如果列表为空,请返回无。

在我查看解决方案之前,我尝试多种不同的方法来解决它,花了几个小时才找到一个与我的解决方案非常相似的解决方案,尽管有一个区别。

我的代码如下:

function likeOrDislike(buttons) {
  let answer = '无';

  for (let i = 0; i < buttons.length; i++) {
    if (buttons[i] !== '无') {
      answer = buttons[i];
    }
  }

  return answer;
}

下面是可行的解决方案:

function likeOrDislike(buttons) {
  let state = '无';

  for (let i = 0; i < buttons.length; i++) {
    if (buttons[i] === state) {
      state = '无';
    } else {
      state = buttons[i];
    }
  }

  return state;
}

所以基本上是用来解决它的相同方法,但是我不明白为什么在可行的解决方案中,必须再次将变量“状态”声明为“无”,尽管它已经在循环外部声明过。

我以为我只需要创建一个条件,即如果 buttons[i] 不等于 '无',然后假设在任何其他情况下它都会默认为 '无',因为它在循环外部已经声明了。

英文:

I am attempting a challenge on codewars as follows:

Story

YouTube had a like and a dislike button, which allowed users to express their opinions about particular content. It was set up in such a way that you cannot like and dislike a video at the same time.

There are two other interesting rules to be noted about the interface: Pressing a button, which is already active, will undo your press. If you press the like button after pressing the dislike button, the like button overwrites the previous "Dislike" state. The same is true for the other way round.

Task

Create a function that takes in a list of button inputs and returns the final state.

Examples

likeOrDislike([Dislike]) =&gt; Dislike
likeOrDislike([Like, Like]) =&gt; Nothing
likeOrDislike([Dislike, Like]) =&gt; Like
likeOrDislike([Like, Dislike, Dislike]) =&gt; Nothing

Notes

If no button is currently active, return Nothing.
If the list is empty, return Nothing

I was stuck trying mutliple different ways to solve it for a couple of hours before checking the solutions and found one very similar to mine, albeit with one difference.

My code is as follows:

function likeOrDislike(buttons) {
  let answer = &#39;Nothing&#39;;

  for (let i = 0; i &lt; buttons.length; i++) {
    if (buttons[i] !== &#39;Nothing&#39;) {
      answer = buttons[i];
    }
  }

  return answer;
}

Working solution below:

function likeOrDislike(buttons) {
  let state = &#39;Nothing&#39;;

  for (let i = 0; i &lt; buttons.length; i++) {
    if (buttons[i] === state) {
      state = &#39;Nothing&#39;;
    } else {
      state = buttons[i];
    }
  }

  return state;
}

So it's pretty much the same method used to solve it, however I don't understand why in the working solution, you have to declare the variable 'state' as 'Nothing' again when it's already been declared outside the loop.

I thought that I could just create one condition for if buttons[i] is not equal to 'Nothing' and then assumed it it would default to 'Nothing' in any other case since it was declared outside of the loop.

答案1

得分: 0

'reduce'非常适合这种情况。如果同一个按钮被按两次,你会得到Nothing。否则,被按的按钮就是新的状态。

const [Nothing, Like, Dislike] = ['Nothing', 'Like', 'Dislike']

function likeOrDislike(seq) {
  return seq.reduce((a,c) => a===c ? Nothing : c, Nothing)
}

console.log(likeOrDislike([Dislike])) // Dislike
console.log(likeOrDislike([Like, Like])) // Nothing
console.log(likeOrDislike([Dislike, Like])) // Like
console.log(likeOrDislike([Like, Dislike, Dislike])) // Nothing
英文:

'reduce' is perfect for this situation. If the same button is pressed twice, you get Nothing. Otherwise, the button pressed is the new state.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const [Nothing, Like, Dislike] = [&#39;Nothing&#39;, &#39;Like&#39;, &#39;Dislike&#39;]

function likeOrDislike(seq) {
  return seq.reduce((a,c) =&gt; a===c ? Nothing : c, Nothing)
}

console.log(likeOrDislike([Dislike])) // Dislike
console.log(likeOrDislike([Like, Like])) // Nothing
console.log(likeOrDislike([Dislike, Like])) // Like
console.log(likeOrDislike([Like, Dislike, Dislike])) // Nothing

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 02:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275379.html
匿名

发表评论

匿名网友

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

确定