GithubHelp home page GithubHelp logo

pizzabot's Introduction

Sprint 2: Pizzabot

Today's assignment is to practices some parts of JavaScript you've learnt so far to create a little text-based bot which you can use to order pizza on your computer.

How to complete this assignment

Follow the tasks below and write your bot code in code/pizzabot.js. You can open code/index.html in the browser to use and test the bot.

The bot you'll build is a fake ordering system which will use alert() and prompt() to allow the user to input orders, and the bot to send output. Read up on how to use them before you continue.

Task 1: Setting up variables

Before you start writing your "bot", there's a few things you should know about your pizzeria:

  • Your pizzeria only serves 3 different pizzas right now; Vegetarian, Hawaiian and Pepperoni.
  • All the pizzas have the same price - 80 SEK.
  • Because of the popularity of your pizzeria, and long queues, you can only order multiple pizzas of the same sort.

1.1 Modeling the menu

To start you off, we've defined 4 variables in code/pizzabot.js which you'll use throughout the rest of this assignment:

const vegetarian = "Vegetarian Pizza"
const hawaiian = "Hawaiian Pizza"
const pepperoni = "Pepperoni Pizza"

const pizzaPrice = 80

1.2 Greeting the customer

In code/pizzabot.js, you can start coding the bot interaction. The first step is to greet a new customer.

Use alert() to print the message "Hey! Happy to serve your pizza. On our menu we have X, Y and Z".

Use the variables vegetarian, hawaiian and pepperoni to replace X, Y and Z in the message so that it reads "Hey! Happy to serve your pizza. On our menu we have Vegetarian Pizza, Hawaiian Pizza and Pepperoni Pizza"

2.3 Ask the user which pizza they want, and how many

Use prompt() to ask the user which pizza they want. The message in the prompt should read Enter the name of the pizza you want to order today.. Their response should be stored in a new variable called orderName.

Next, ask the user (with another prompt()) how many they'd like: How many of X do you want?. X should be replaced with the orderName variable so that it if I ordered "Hawaiian Pizza" it would read "How many of Hawaiian Pizza do you want?". As before, the response from this prompt should be stored in a variable, this time called orderQuantity

2.4 Finalizing the order

Calculate the total price of the order using the orderQuantity and pizzaPrice variables and store it in a variable called orderTotal. Use an alert to tell the user: Great, I'll get started on your X right away, it will cost Y kr (where X should be replaced by the orderName variable and Y should be replaced by the orderTotal variable).

For example, if I ordered 3 Hawaiian pizzas, the message should read: "Great, I'll get started on your Hawaiian Pizza right away, it will cost 240 kr"

3. Conditionals

We want you to add three conditionals statements (if-statements) to your program to improve it.

3.1 Checking that the Pizza is on the menu

When the user enters the orderName prompt, add an if-statement to check if the input text matches any of the pizza name variables (vegetarian, hawaiian, pepperoni).

Show cooking times

Before you print the final Great, I'll get started on your X right away, it will cost Y kr message to the user, calculate the cooking time based on these rules and add it to the message:

  • 1-2 pizzas: The pizzas will take 10 minutes.
  • 3-5 pizzas: The pizzas will take 15 minutes.
  • 6+ pizzas: The pizzas will take 20 minutes.

For example, if I ordered 3 Hawaiian pizzas, the message should now read: "Great, I'll get started on your Hawaiian Pizza right away, it will cost 240 kr. The pizzas will take 15 minutes."

4. Functions

Finally, clean up your code by moving some code into functions and then calling that function when needed.

The functions should all reflect the tasks we've completed so far. So, the functions should be:

  1. checkOrderName() which should take the orderName variable as an argument and return true or false if the pizza is on the menu or not.
  2. totalCost() which takes orderQuantity as an argument and returns the total cost for the order.
  3. cookingTime() which takes orderQuantity and returns the number of minutes it will take to finish the order.

5. Hand in assignment

As always, when you're done, submit a pull request on GitHub (from your repository into the technigo one) for review. Read the guide on GitHub for more info on how to do this.

๐Ÿ“š Reading List


๐Ÿ†˜ How to get help

Learning how to think as a web developer is learning how to be an expert in problem solving. So whenever you get stuck start with step 1 and continue until problem solved.

  1. Google! In English, type in the error message if there is one, search within the language your're using (ie CSS, JavaScript etc).
  2. Ask your code buddies in your Company.
  3. Ask your fellow students in Slack.
  4. Ask Damien or David. Please note: we are part of a sharing community - share the answer with your fellows.

๐Ÿ’ฅ Success!

After completing this assignment your should:

  • Be comfortable using variables
  • Know some string methods to modify strings
  • Understand control flow and the use of conditionals statements
  • Be able to write basic functions which take arguments and return values

๐Ÿƒ Stretch Goals

Done with the main task? Here's some ideas for things to continue with:

  1. Go to the Wikipedia Article about Hawaiian Pizza and:
  • Copy the first three paragraphs. Store the text in a String
  • Make your program count the number of words in the string
  • Make your program count the number of times the word pineapple appears.
  1. Instead of having your pizzabot live in the console make a form in HTML for the input from the user, pizza type and number of pizzas. Then make your program print it's response in HTML instead.

A combination of these two examples will help you on the way: Forms submissions with JavaScript and Get Element by ID.

pizzabot's People

Contributors

carrolita avatar idlefingers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.