【问题标题】:Keep changes to a variable from an if statement保留 if 语句中对变量的更改
【发布时间】:2020-05-11 21:16:10
【问题描述】:

我试图弄清楚如何更改 if 语句中的变量并使其保持全局不变。

用户输入:!change Hi

var A = "Hello"

if (msg.content.includes ('!change')) {
  A = msg.content.replace('!change ', '');
}
msg.send(A); //the change won't stick outside the if statement

输出:你好

我做了一些阅读并尝试使用一个函数,但我也无法让它工作。

var A = "Hello"

if (msg.content.includes ('!change')) {
  B = msg.content.replace('!change ', '');

  changetext(B)
}


function changetext(altText){
A=altText
}

msg.send(A); //the change won't stick here either

对不起,如果我没有说得太清楚,我正在尽力而为。我的目标是让用户可以输入“!change Hi”,然后更改将保留在 if 语句和 A="Hi" 之外。希望这是有道理的。任何建议将不胜感激。

【问题讨论】:

    标签: javascript node.js bots


    【解决方案1】:

    尝试运行此解决方案,我认为它已经满足您的需求,如果没有,请说明 cmets 中的输出有什么问题

    var A = "Hello";
    var msg = {
    content:'!cahnge Hello'
    }
    
    if (msg.content.includes ('!change')) {
      A = msg.content.replace('!change ', '');
    }
    console.log(A);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2017-05-16
      相关资源
      最近更新 更多