简体   繁体   English

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

[英]How to change a message in a boolean variable in node.js

I'm trying to modify a code so when a LED turn on and off, the program will show an Active-Inactive message, instead of a classic boolean True-false. 我正在尝试修改代码,以便在打开和关闭LED时,程序将显示Active-Inactive消息,而不是经典的布尔值True-false。

I'm currently using this code in a Beaglebone Black, I'm new in Node.js, and I found it in a book (Exploring BeagleBone), when I ran it like it was in the book it worked showing a message of: Led is: true, Led is: false , but when I tried to modify it to show active-inactive, it didn't work. 我目前正在Beaglebone Black中使用此代码,是Node.js的新手,并且在一本书(探索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
}

I expect the output of this code as: LED is: active, LED is: inactive, LED is: active, LED is: inactive , and so on, but the actual output just shows the first as active and the rest as inactive . 我希望此代码的输出为: 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
}

You changed variable isOn from boolean value true or false to string "active" or "inactive" , which would be considered as true the next call. 您将变量isOn从布尔值truefalse更改为字符串"active""inactive" ,在下一次调用时将其视为true That's why the rest will all be inactive . 这就是为什么其余所有都将inactive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM