Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch string for numbers
    primarykey
    data
    text
    <p>I have a javascript chat bot where a person can type into an input box any question they like and hope to get an accurate answer. I can do this but I know I'm going about this all wrong because I don't know what position the number will appear in the sentence. If a person types in exactly:</p> <p>what's the square root of 5 this works fine. </p> <p>If he types in things like this it doesn't. </p> <p>what is the square root of the 5</p> <p>the square root of 5 is what</p> <p>do you know what the square root of 5 is</p> <p>etc</p> <p>I need to be able to determine where the number appears in the sentence then do the calculation from there. Note the line below is part of a bigger working chatbot. In the line below I'm just trying to be able to answer any square root question regardless of where the number appears in the sentence. I also know there are many pitfalls with an open ended input box where a person can type anything such as spelling errors etc. This is just for entertainment not a serious scientific project. :) </p> <pre><code>if( (word[0]=="what's") &amp;&amp; (word[1]=="the") &amp;&amp; (word[2]=="square") &amp;&amp; (word[3]=="root") &amp;&amp; (word [4]=="of") &amp;&amp; (input.search(/\d{1,10}/)!=-1) &amp;&amp; (num_of_words==6) ){ var root= word[5]; if(root&lt;0){ document.result.result.value = "The square root of a negative number is not possible."; }else{ word[5] = Math.sqrt(root); word[5] = Math.round(word[5]*100)/100 document.result.result.value = "The square root of "+ root +" is "+ word[5] +"."; } return true; } </code></pre> <p>Just to be clear the bot is written using "If statemments" for a reason. If the input in this case doesn't include the words "what" and "square root" and "some number" the line doesn't trigger and is answered further down by the bot with a generic "I don't know type of response". So I'm hoping any answer will fit the format I am using. Be kind, I'm new here. I like making bots but I'm not much of a programmer. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload