JavaScript Lesson 3 - Word Search
JavaScript Lesson 3 - Word Search
Overview
Introduction
In this lesson we are going to use HTML, CSS and JavaScript to create a word search game that highlights
words when the user gets it correct!
Prerequisites
We recommend finishing the following lessons before starting this one if you are new to the App Builder:
Learning Objectives
By the end of this lesson:
New App
As always, the first thing we need to do is create a new app for our project and give it a name. Make sure
you're logged into AppShed and navigate to the App Builder.
● Click Settings
● In the text box named Name type Word Search!
● Click the Green Save button
Creating a Table
The letters for our word search will be contained in a 3x3 table which we will add to our app using the
<table> tag in HTML.
Here is where we will put our HTML to create a table, we want a 3x3 table that looks like this:
A P P
V E S
D E H
(Later on you can add your own letter but for now use the ones we have to make sure your code works!)
● Click Save
Breaking Down the HTML
See if you can figure out what's going on with this quick hint:
What we have just done is created a table with three rows (3x <tr>) and three columns (each row has 3x
<td>) and finally we put a letter into each column.
The most important thing we did however is give each cell in our table a class name, we did this by
putting class="x" in each <td> tag, like this <td class="1">A</td>.
You should notice that each letter has its own class name from 1-9. So A = Class 1 and H = Class 9.
This is going to help us later when we start coding JavaScript.
A little CSS
Our table isn't quite looking like a table yet, the letters are in a grid which is correct but there are no defining
borders! We will add these borders with CSS.
● Click on Settings
● Click Advanced
Here we can see the Custom CSS editor, this is where we can apply some CSS to our table.
● Click Save
What we have just done with the CSS is apply a border to all <td> tags in our app and then we put a
padding of 15 pixels inside the <td> or table cell to make them more square.
If you would like to learn more about creating tables with HTML and styling them with CSS check out the
HTML Table example in the HTML & CSS Reference Section.
Rules:
● Click Save
Coding
The If Statement
We should be nice and comfortable with the IF Statement by now as we are going to use it the exact same
way we have in previous lessons, in order to evaluate the status of our input box.
if (app.getVariable('word') == 'APPSHED') {
//output
}
In the code above we are stating that if the user types APPSHED something will happen in the output. We
will come back to our condition in a moment but for now let's set up all of our If statements, we will need an
IF statement for each available word in our table.
(There are actually 49 words in this simple 3v3 table, we will do four together, the rest is for you to try and
find!)
We can add our next IF statement as an ELSE IF statement, this is used to check another condition if the
first one returns false.
if (app.getVariable('word') == 'APPSHED') {
//output
}
else if (app.getVariable('word') == 'APP') {
//output
}
else if (app.getVariable('word') == 'DEAP') {
//output
}
else if (app.getVariable('word') == 'PAVED') {
//output
} else {
alert('nope, well maybe, we did not add them all');
}
As you can see we can add as many ELSE IF Statements as we want. Once we have added all that we
want to, we can end it with an ELSE statement. The ELSE statement checks if all other statements are false
and if they are it is activated, in its output we add an alert which will tell the user they didn't input a correct
(or known) word.
The Output
Now that we have added all of our conditional statements it's time to add our output. What we want our
output to do is to highlight the relevant cells of the table depending on which word the user types, luckily we
gave each cell a class name earlier making it easy for us now!
We will use jQuery to get the relevant cells and then a jQuery method called .css to apply CSS to them.
The CSS we apply will simply be background-color: blue;
First Word
Let's start with our first word, APPSHED. When the user type this and clicks submit the 1st, 2nd, 3rd, 6th,
7th, 8th and 9th cell should light up. See the little cheat sheet bellow if this is confusing
jQuery('.1,.2,.3,.6,.7,.8,.9').css('background-color','red')
If this is what the code for APPSHED would look like, what would the code for the rest look like, see if you
can figure it out yourself!
Here is what your final code should look like:
if (app.getVariable('word') == 'APPSHED') {
jQuery('.1,.2,.3,.6,.9,.8,.7').css('background-color','red')
}
else if (app.getVariable('word') == 'APP') {
jQuery('.1,.2,.3').css('background-color','blue')
}
else if (app.getVariable('word') == 'DEAP') {
jQuery('.7,.5,.1,.2').css('background-color','yellow')
}
else if (app.getVariable('word') == 'PAVED') {
jQuery('.2,.1,.4,.5,.7').css('background-color','purple')
} else {
alert('nope, well maybe, we did not add them all');
}
Give it a Test
Let's now give it a test.
Conclusion
● Click Publish
● Click Start
As soon as we get a thumbs up our app is ready to view.
● Click Share
● Click QR Code
● Scan the QR Code with your phone
Once the code is scanned on your phone you can share this app by sending the link.
What we covered
At this point we have a fully working word search puzzle that highlights the words to show users what words
they have found already! We did this by using HTML, CSS and finally JavaScript with the jQuery library to
bring it all together
Why don't you see if you can make your own Word Search puzzles now ? Maybe even try to make a 4x4 one
instead!