如何在 node.js 中更改布尔变量中的消息

How to change a message in a boolean variable in node.js

我正在尝试修改代码,以便当 LED 打开和关闭时,程序将显示活动-不活动消息,而不是经典的布尔值 True-false。

我目前在 Beaglebone Black 中使用此代码,我是 Node.js 的新手,我在一本书(Exploring BeagleBone)中找到它,当我 运行 喜欢它时在书中它显示了一条消息:Led is: true, Led is: false,但是当我试图修改它以显示活动-非活动时,它没有工作。

function toggleLED()
{
    isOn = !isOn    // invert the isOn state in each call
    if (isOn) b.digitalWrite(LED3Pin, 1); // light the led
    else b.digitalWrite(LED3Pin, 0); // turn off the led
    if (isOn) isOn = 'active'; // changing default TRUE message for active
    else isOn = 'inactive'; // changing default FALSE message for inactive
    console.log('LED is: ' + isOn); // show the state
}

我希望此代码的输出为:LED is: active, LED is: inactive, LED is: active, LED is: inactive,依此类推,但实际输出仅显示第一个为 active,其余为 inactive

function toggleLED()
{
    isOn = !isOn    // invert the isOn state in each call
    if (isOn) b.digitalWrite(LED3Pin, 1); // light the led
    else b.digitalWrite(LED3Pin, 0); // turn off the led
    console.log('LED is: ' + isOn ? 'active' : 'inactive'); // show the state
}

您将变量 isOn 从布尔值 truefalse 更改为字符串 "active""inactive",这将被视为 [=12= 】 下一个电话。这就是为什么其余的都是 inactive.