+ 8

I'm curious if there's a shorter way to code what I have here with the same outcome. Any pointers would be greatly appreciated.

I'm using the .includes method in JavaScript, and my code is already a bit bulky, especially seeing as I plan to scale it. I tried 'or' and ||, but they didn't function properly. Any tips? https://code.sololearn.com/W9SW79XTzct0/?ref=app

28th Jan 2019, 8:23 PM
Charlie Mitchell
Charlie Mitchell - avatar
11 odpowiedzi
+ 8
You're absolutely right. Thanks John!
28th Jan 2019, 10:12 PM
Charlie Mitchell
Charlie Mitchell - avatar
+ 8
Thanks, Mert. I'll give that a try.
29th Jan 2019, 12:43 AM
Charlie Mitchell
Charlie Mitchell - avatar
+ 7
Good suggestion, Maneren. I actually just implemented that approach, but I want to be able to make a long list of potential inputs to which the Bot would have the same response without having to have multiple 'if' statements. Any thoughts?
28th Jan 2019, 8:54 PM
Charlie Mitchell
Charlie Mitchell - avatar
+ 7
Oh, that's a good idea, Maneren! I'll give that a whirl a little later. I've got to get back to work soon. I'll let you know how that goes.
28th Jan 2019, 9:07 PM
Charlie Mitchell
Charlie Mitchell - avatar
+ 7
I tried to use the ||, joel. But it didn't function properly. I'm going to try to give a switch statement a go, see how that works for me.
28th Jan 2019, 10:15 PM
Charlie Mitchell
Charlie Mitchell - avatar
+ 6
Using regex is probably the best solution if((/hi|hello/gi).test(human)) alert("Hello") else if((/bye/gi).test(human)) alert("Goodbye")
28th Jan 2019, 10:28 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
Try getting of letters ceses by .toLowerCase() method to have just one statement for both "Hello" and "hello".
28th Jan 2019, 8:47 PM
Maneren
Maneren - avatar
+ 2
Maybe try switch statement
28th Jan 2019, 9:05 PM
Maneren
Maneren - avatar
+ 2
Since goodbye includes bye, you don't need to test it. You should add something to cover finding no matches such as "I'm sorry, but I haven't been trained to handle that as yet."
28th Jan 2019, 10:01 PM
John Wells
John Wells - avatar
+ 1
if (human.includes('hi') || ('hello')){ alert('Hi, can I help you pick a destination?');
28th Jan 2019, 9:37 PM
joel
+ 1
use || for next goodbye statement
28th Jan 2019, 9:37 PM
joel