Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manipulate a string in javascript?
    primarykey
    data
    text
    <p>I'm new to javascript and need some help! I want to make a magic 8-ball script that pulls a specific answer for certain questions. So far I have it so that if I ask a specific question, it will give me one answer, but I want to alter it so that it will give me a specific answer based on whether or not the question uses 'who' or 'what' or if a question uses an 'a' 'd' and 'b' and things like that. Does anyone know how to do this and could you possibly help me? I'm completely stuck and don't know how to go about this. Thank you for your help!</p> <pre><code> &lt;script&gt; function eightBall() { var answer = document.getElementById("answerBox").value; var images = ['answerYes.png', 'answerNo.png', 'answerAskLater.png', 'answerNotL.png', 'answerMaybe.png']; if (answer.search(/Will I be rich?/) &gt; -1) { var askLater = '../images/answerAskLater.png' document.getElementById('eightTemplate').src = askLater; } else if (answer.search(/Will I be happy?/) &gt; -1) { var no = '../images/answerNo.png' document.getElementById('eightTemplate').src = no; } else if (answer.search(/Will I live to be 90 years old?/) &gt; -1) { var yes = '../images/answerYes.png' document.getElementById('eightTemplate').src = yes; } else if (answer.search(/Will I be married later in life?/) &gt; -1) { var notLikely = '../images/answerNotL.png' document.getElementById('eightTemplate').src = notLikely; } else if (answer.search(/Can I have a puppy?/) &gt; -1) { var maybe = '../images/answerMaybe.png' document.getElementById('eightTemplate').src = maybe; } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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