<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ask 8 ball</title>
    <description>The latest articles on DEV Community by Ask 8 ball (@ask8ballfree).</description>
    <link>https://dev.to/ask8ballfree</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4283967%2F31056c75-dc3e-428d-83da-732d38ebad7d.jpg</url>
      <title>DEV Community: Ask 8 ball</title>
      <link>https://dev.to/ask8ballfree</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ask8ballfree"/>
    <language>en</language>
    <item>
      <title>How I Built a Magic 8 Ball in JavaScript (No Backend, No Storage)</title>
      <dc:creator>Ask 8 ball</dc:creator>
      <pubDate>Sat, 05 Dec 2026 05:13:47 +0000</pubDate>
      <link>https://dev.to/ask8ballfree/how-i-built-a-magic-8-ball-in-javascript-no-backend-no-storage-37f9</link>
      <guid>https://dev.to/ask8ballfree/how-i-built-a-magic-8-ball-in-javascript-no-backend-no-storage-37f9</guid>
      <description>&lt;p&gt;Disclosure: I run &lt;a href="https://ask8ballfree.com" rel="noopener noreferrer"&gt;Ask8BallFree&lt;/a&gt;, a free online Magic 8 Ball. I built it as a small front-end project, and this post explains how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea
&lt;/h2&gt;

&lt;p&gt;A Magic 8 Ball is a good first JavaScript project because the whole interaction is simple: ask a yes-or-no question, shake the ball, and show one answer. There is no account, no database, and nothing to save.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three pieces
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;HTML&lt;/strong&gt; — a question input, a button, and a place to show the answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSS&lt;/strong&gt; — the round ball, the dark window in the middle, and a short shake animation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; — the list of answers and the random choice.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The answers
&lt;/h2&gt;

&lt;p&gt;I kept the classic set of twenty answers, split into positive, uncertain, and negative ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;answers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="s2"&gt;"It is certain."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Without a doubt."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Yes, definitely."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Most likely."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Ask again later."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Cannot predict now."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Do not count on it."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"My sources say no."&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is only a sample — the full list follows the same pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking one at random
&lt;/h2&gt;

&lt;p&gt;The core of the project is three lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;randomIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;randomIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;answerElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Math.random()&lt;/code&gt; gives a number from 0 up to (but not including) 1. Multiplying by the number of answers and rounding down with &lt;code&gt;Math.floor()&lt;/code&gt; gives a valid index every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small detail that matters
&lt;/h2&gt;

&lt;p&gt;Before showing an answer, I check that the question is not empty. If it is, I ask the user to type a question first. I also clear the old answer while the ball is shaking, so the new one feels like a fresh result.&lt;/p&gt;

&lt;p&gt;For the shake, I add a CSS class, wait about half a second, then remove the class and show the answer. No library is needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;The interesting part was not the random choice. It was keeping the project small: one page, plain JavaScript, no backend, and no storage. That made it easy to test, easy to share, and easy to keep free.&lt;/p&gt;

&lt;p&gt;If you want a beginner project with a visible result in an evening, a Magic 8 Ball is a good choice. Start with the answers, add the random pick, and then make the ball look and feel fun.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>fun</category>
    </item>
  </channel>
</rss>
