Sitemap
Manuel Espinoza

Manuel Espinoza

console.log(“help_me”);

4 min readApr 7, 2018

Coding in JavaScript is one of the languages where you are constantly going back and forth to check if there is anything happening. I have gone multiple projects using basic JavaScript functions and loops. Throughout my ventures, the one thing that I take away from everything is to use semicolons and to insert console.log practically everywhere. It can be a living hell debugging, and if you really don’t know exactly what you’re doing (typically me) you’re bound to fail. Alternatively, if you’re lost the best thing to do is check your code, and plug a few console.logs in and see what gets outputted. Beyond knowing what I do know so far that is the only way I verify if I know what I am doing.

You can honestly begin with any project, and just use trial and error to understand what is happening. Most of the time I can get in deep and check my syntax which is usually involving a misuse of a variable whether it is in the wrong place, or I forget to reference an element in my HTML. Besides the point, you can check the state of your code by inserting it into functions.

One of the projects that I was working on involved making a few functions that would allow me to choose the toppings of a pizza, choose the crust, and in the end, I would assemble everything. I was dealing with JavaScript Objects, which deal with properties and methods. I am able to simply create my pizza variable, and create my objects (thin crust, pepperoni, 1 cup of flour, and medium size).

var pizza = {
crustType: “thin crust”,
toppingType: “pepperoni”,
sizeType: “medium”,
flourAmt: “1 cup of flour”,

Having my default objects, I need to create some functions to provide alternative options for our users. From here, this is where I actually want to start testing my functions to make sure they work. My first function makePizza has the variable pizzaText whose purpose is to display our completely built pizza. We are defining our variable as a string (manipulative text) that includes our objects. We are able to check the console throughout the process to make sure our code works.

makePizza: function() {
var pizzaText = “We pull out of the oven a “+pizza.sizeType+” “+pizza.toppingType+” pizza with “+pizza.crustType+”. Please enjoy.”;
console.log(pizzaText);
document.getElementById(“feedback”).innerHTML = pizzaText;
}, //end of the method
This is a look at what the console will output if working correctly

To insert our string into our web page we will need to use a line of code to reference a specific ID in our HTML called “feedback”, which has been linked to a div tag. If we take a quick look at our HTML we can see that our site consists mainly of headings, buttons, and divisions (div).

Get Manuel Espinoza’s stories in your inbox

Join Medium for free to get updates from this writer.

JavaScript

document.getElementById(“feedback”).innerHTML = pizzaText;

HTML

<div id=”feedback”>feedback</div>

We also made a function to create a shopping list that would tell us what ingredients we should get to make this pizza. With this function it is relying on what objects get selected, so if thick crust is chosen it will change the ingredients to display 2 cups of flour instead. After that we will add our line of code to link to our feedback ID, and we should be able to display our ingredients when we click our Shopping List button.

shopList: function() {
if (pizza.crustType == “thick crust”) {
pizza.flourAmt = “ 2 cups of flour”;
}
var shopText = “To bake this pizza we will need “+pizza.flourAmt+” and 1 pound of “+pizza.toppingType+”.”;
console.log(shopText);
document.getElementById(“feedback”).innerHTML = shopText;
}//end of method
Press enter or click to view image in full size
A look at the finished project

If you’re looking at the website, you might be wondering “what about the Pineapple and Large size options?” Well if we take another look at our HTML you can see in the syntax that we have and onclick. What this does to the functions is change the value of the object value to whatever we set it to. So under our “Topping Options” we have our Pineapple button to change the value of pizza.toppingType to ‘pineapple’ when clicked. Same goes for our pizza.sizeType. These onclick buttons could easily have been implemented in our JavaScript, but I want to show a couple of different methods of creating these functions.

<h2>Topping Options</h2><button onclick=”pizza.toppingType=’pepperoni’”>Pepperoni</button>
<button onclick=”pizza.toppingType=’pineapple’”>Pineapple</button>

After a bit of testing I was able to get my functions working, and I got my toppings and sizes to build my pizza. Throughout my project, I plugged in console.log into my functions, variables, and objects to make sure everything was working correctly. I was able to find mistakes easily, and even got frustrated trying to understand why things weren’t working (it came down to a single semicolon). Overall it was useful testing my code often.

Manuel Espinoza is a student in the Digital Media program at Utah Valley University, Orem Utah, studying Web & App Development. The following article relates to (Pizza Emporium) in the (DGM 2760 Course) and representative of the skills learned.

--

--