question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

switch statement breaks string comparison

See original GitHub issue

I hope I’m not going nuts, this really seems “off the wall” - what am I missing?

I have code in a node which works 100% as expected but if I include a switch statement above it, it produces bizarre results.

What are the steps to reproduce?

let repo='/home/pi/H4firmware'
let hardcoded='/home/pi/H4firmware/H4.ino.d1_mini.bin'
let bin1
let bin2
let type='WEMOS_D1MINI'
bin1='d1_mini'
switch(type){
    case 'WEMOS_D1MINI':
        bin2='di_mini';
        break;
}
//bin2='d1_mini' // comment out this line and msg is '...nuts' 
let f1=`${repo}/H4.ino.${bin1}.bin`
let f2=`${repo}/H4.ino.${bin2}.bin`
if(f1!==f2) node.warn('completeley nuts')
else node.warn('perfectly normal')
node.warn('f1='+f1+' '+typeof f1+' L='+f1.length)
node.warn('f2='+f2+' '+typeof f2+' L='+f2.length)
node.warn(f1===hardcoded) // true OF COURSE!
node.warn(f2===hardcoded) // false ???

return msg;

What do you expect to happen?

I expect f1===f2=== hardcoded and that the msg"completely normal" should appear

Result with bin2 assignment commented out

4/3/2020, 11:43:06 PMnode: Works OK
function : (warn)
"completeley nuts"
4/3/2020, 11:43:06 PMnode: Works OK
function : (warn)
"f1=/home/pi/H4firmware/H4.ino.d1_mini.bin string L=38"
4/3/2020, 11:43:06 PMnode: Works OK
function : (warn)
"f2=/home/pi/H4firmware/H4.ino.di_mini.bin string L=38"
4/3/2020, 11:43:06 PMnode: Works OK
function : (warn)
true
4/3/2020, 11:43:06 PMnode: Works OK
function : (warn)
false

Result with bin2 assignment overriding value set inside switch


4/3/2020, 11:49:06 PMnode: Works OK
function : (warn)
"perfectly normal"
4/3/2020, 11:49:06 PMnode: Works OK
function : (warn)
"f1=/home/pi/H4firmware/H4.ino.d1_mini.bin string L=38"
4/3/2020, 11:49:06 PMnode: Works OK
function : (warn)
"f2=/home/pi/H4firmware/H4.ino.d1_mini.bin string L=38"
4/3/2020, 11:49:06 PMnode: Works OK
function : (warn)
true
4/3/2020, 11:49:06 PMnode: Works OK
function : (warn)
true

Please tell us about your environment:

image

  • [v1.0.0 ] Node-RED version:
  • Node.js version:
  • npm version:
  • Platform/OS:
  • Browser: Google Chrome Version 80.0.3987.149 (Official Build) (64-bit) on Windows7

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tmdoit-zzcommented, Apr 3, 2020

@knolleary typo inside switch statement: bin2=‘di_mini’; > bin2=‘d1_mini’;

1reaction
philbowlescommented, Apr 3, 2020

And thanks @tmdoit for his better eyes! 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strings in switch Statements
The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using...
Read more >
Why can't I use switch statement on a String? - java
Firstly comparing two strings can be a fairly lengthy procedure, adding to the performance problems that are mentioned above. Secondly if there is...
Read more >
Can we use Switch statement with Strings in java?
Comparison of Strings in switch statement is case sensitive. i.e. the String you have passed and the String of the case should be...
Read more >
String in Switch Case in Java - GeeksforGeeks
3. Case Sensitive Comparison: The switch statement compares the String object in its expression with the expressions associated with each case ...
Read more >
The "switch" statement - The Modern JavaScript Tutorial
A switch statement can replace multiple if checks. It gives a more descriptive way to compare a value with multiple variants.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found