+ 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
11 ответов
+ 8
You're absolutely right. Thanks John!
+ 8
Thanks, Mert. I'll give that a try.
+ 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?
+ 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.
+ 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.
+ 6
Using regex is probably the best solution
if((/hi|hello/gi).test(human))
alert("Hello")
else if((/bye/gi).test(human))
alert("Goodbye")
+ 2
Try getting of letters ceses by .toLowerCase() method to have just one statement for both "Hello" and "hello".
+ 2
Maybe try switch statement
+ 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."
+ 1
if (human.includes('hi') || ('hello')){
alert('Hi, can I help you pick a destination?');
+ 1
use || for next goodbye statement