CMPT 100 : INTRODUCTION TO

Similar documents
CMPT 100 : INTRODUCTION TO

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

c122mar413.notebook March 06, 2013

Working with JavaScript

By the end of this section of the practical, the students should be able to:

COMS 469: Interactive Media II

Introduction to Programming in Turing. Input, Output, and Variables

ITEC136 - Lab 2 Population

Variables and Typing

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

ORB Education Quality Teaching Resources

Anatomy of a Function. Pick a Name. Parameters. Definition. Chapter 20: Thinking Big: Programming Functions

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Part III Appendices 165

Lab 2 Population. Purpose. Assignment Lab 2 analyzes population growth of a town as well as compare the population growth of two towns.

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

COMS 469: Interactive Media II

Ph3 Mathematica Homework: Week 1

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

CSC Web Programming. Introduction to JavaScript

HTML5 and CSS3 More JavaScript Page 1

Lab 8 CSE 3,Fall 2017

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Chapter 2 Working with Data Types and Operators

BB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class:

Section 1. How to use Brackets to develop JavaScript applications

var num1 = prompt("enter a number between 1 and 12","1"); var promptstring = "What is " + num1 + " times 3?"; var num2 = prompt(promptstring);

Lesson 3: Basic Programming Concepts

Decisions, Decisions. Testing, testing C H A P T E R 7

C++ Support Classes (Data and Variables)

introjs.notebook March 02, 2014

Iteration and Arrays Dr. Abdallah Mohamed

An Introduction to Python

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Project 2: After Image

Week 1: Introduction to R, part 1

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

The second statement selects character number 1 from and assigns it to.

Python Intro GIS Week 1. Jake K. Carr

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Lab 7: OCaml 12:00 PM, Oct 22, 2017

function < name > ( < parameter list > ) { < statements >

Python Unit

COMP : Practical 6 Buttons and First Script Instructions

Lab 1. Purpose. Assignment. Action Items/Programming Requirements

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Arrays Structured data Arrays What is an array?

Happy Learn JavaScript Tutorial Vol 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

JavaScript Introduction

Lesson 1: Writing Your First JavaScript

Lab 3 - Pizza. Purpose. Assignment

Client Side JavaScript and AJAX

1) Log on to the computer using your PU net ID and password.

Eclipse Environment Setup

CSCI 201 Lab 1 Environment Setup

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CMSC 201 Computer Science I for Majors

Unit E Step-by-Step: Programming with Python

A340 Laboratory Session #5

S A M P L E C H A P T E R

Coding in JavaScript functions

Introduction to C Programming. What is a C program?

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Excel 2010 Functions. 4/18/2011 Archdiocese of Chicago Mike Riley

Intro. Scheme Basics. scm> 5 5. scm>

Class #1. introduction, functions, variables, conditionals

Getting Started with the FTC SDK. Jaxon Brown August 5, 2017

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

C# Programming Tutorial Lesson 1: Introduction to Programming

How to Open a Free Yahoo! Account & Basics

Programming Fundamentals and Python

Lab 1 Concert Ticket Calculator

AUTOMATOR REFERENCE MANUAL

CISC 110 Week 3. Expressions, Statements, Programming Style, and Test Review

INTRODUCTION TO JAVASCRIPT

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Your First Windows Form

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

Programming Paradigms

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CSCE 120: Learning To Code

IAT 445 Lab 10. Special Topics in Unity. Lanz Singbeil

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

Like most objects, String objects need to be created before they can be used. To create a String object, we can write

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Transcription:

CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME 1 By Wendy Sharpe

BEFORE WE GET STARTED... If you have not been to the first tutorial introduction JavaScript then you must catch up on your own time Log onto the lab computer (in Windows) Create a folder for this tutorial on H:// drive Log onto Moodle and open tutorial 5 notes Click on guessinggame.html link and save to your newly created folder Open Notepad++ Start All Programs Course Specific Applications Notepad+++ 2

TUTORIAL 5 OVERVIEW The Guessing Game Algorithm Overview of JavaScript s Math class Alert() function vs Prompt() function While loop general structure and condition if-else : filling out the body of the while loop if-else : ending the game Debugging JavaScript 3

4 THE GUESSING GAME ALGORITHM

SETTING UP THE ALGORITHM FOR THE GAME computer picks a number while user hasn't guessed correctly and they haven't reached 7 guesses get a guess from the user if number is too high, say so if number is too low, say so increment number of guesses by 1 if they've guessed correctly, display congratulations else display game over message TIP: typing the algorithm into your assignment as comments will ensure you get the 2 full marks for properly commenting your code. 5

PICKING VARIABLES FROM THE CODE computer picks a number while user hasn't guessed correctly and they haven't reached 7 guesses get a guess from the user if number is too high, say so if number is too low, say so increment number of guesses by 1 if they've guessed correctly, display congratulations else display game over message Set up 3 variables in your script for the guessing game follow good naming conventions see tutorial 4 notes 6

QUICK REFRESHER ON VARIABLES You MUST first declare the variable before you can use it in your code E.g., var mynumber Variable names are case sensitive (mynumber and MYNumber are two different variables) Variable names must begin with a letter or the underscore character For more information and additional help understanding variables: http://www.w3schools.com/js/js_variables.asp 7

8 JAVASCRIPT S MATH CLASS

MATH CLASS Math.random() Doesn t take any arguments, brackets are empty Generates a random number like 0.8963775077184463 you can prove to yourself that it works by putting it into your code BUT this isn t an integer! So we need to round it off Math.round() Used for rounding off numbers, using it, any supplied argument is rounded off to the nearest integer E.g., Math.round(25.9) //returns 26 Takes one argument One is an integer argument, E.g., 1, 3, 10, 1000 etc. Integers should show up as red in highlighted syntax on Notepad++ var number = Math.round(Math.random()*100); 9

HELP! ERROR CONSOLE MESSAGES Error: math is not defined Source File: file:///h:/cmpt100tutorial5/ guessinggame.htmlline: 11 If you get an error like this, check: Spelling Proper capitalization Semi-colons 10

11 ALERT() VS PROMPT()

ALERT() FUNCTION Display the instructions for the game using the alert() function alert( my text for the alert goes in here"); For more reading on the alert() function: http://www.mediacollege.com/internet/javascript/ basic/alert.html Go ahead and set up the first alert for the game 12

REMEMBER THE PROMPT() FUNCTION FROM LAST WEEK S TUTORIAL? General structure: prompt( this is your physical prompt text, default ); Good programming practice is to always leave a default value in your prompt Use = to assign the value of the prompt to the variable that you are prompting the user for E.g., animal = prompt("enter a kind of animal","duck"); Go ahead and use the prompt() function to ask the user to enter a number don t forget the default value don t forget the put the value into one of your three variables! we don t have strings this time, so what will our default value look like? 13

14 WHILE() LOOP

WHILE LOOP STRUCTURE General structure If you put a semi-colon at the end of the while loop the computer will assume that the loop is finished and the body of the while loop won t execute while(some condition); { // my code goes here } vs. while(some condition) { // my code goes here } 15

HOW DO YOU FIGURE OUT WHAT CONDITION TO USE FOR YOUR LOOP? you need to figure out what conditions need to be true in order for the loop to keep executing I.e., refer back to your algorithm Refine your algorithm into code for the outside part of the loop: while user hasn't guessed correctly and they haven't reached 7 guesses while (user hasn't guessed correctly and they haven't reached 7 guesses) while (user hasn't guessed correctly AND they haven't reached 7 guesses) while (usernumber!= number && numberguesses < 7) 16

IF-ELSE : FILLING OUT THE BODY OF THE WHILE LOOP 17

IF General structure of the if-statement: if( true ) { document.write( hello, world! ); } OR if( true ) document.write( hello, world! ); The second case only works when there will be one single line of code after the if( true ) 18

ELSE General structure of the else: Still use brackets No need to use round parentheses for the else if(true) { } else { } document.write( hello, world! ); // unlike the if, the else doesn t get a condition in parentheses. document.write( goodbye, world! ); for more help in understanding if-else, visit: http://www.w3schools.com/js/js_if_else.asp 19

USING OUR ALGORITHM TO FILL IN THE BODY OF THE WHILE LOOP Go ahead and fill in the while loop using the algorithm from the tutorial notes if you re really struggling to understand while loops, check out: http://www.w3schools.com/js/js_loop_while.asp body of the while loop from our algorithm: get a guess from the user if number is too high, say so if number is too low, say so increment number of guesses by 1 NOTE: we already asked the user to enter a value, and we incremented our numberguesses counter. What does this mean for the order of the code? Should we ask for another guess before or after we check the first guess 20

21 IF-ELSE : ENDING THE GAME

FILL IN THE END-GAME WITH IF-ELSE STRUCTURE if they've guessed the number correctly, they've won the game using document.write( ), tell them they've won using document.write( ), confirm the correct answer and tell them how to restart the game change the background color of the page to yellow, using document.bgcolor="yellow" else the game is over because they have no guesses left. using document.write( ), tell them the game is over using document.write( ), give them the correct answer and tell them how to restart the game change the background color of the page to red, using document.bgcolor="red";. 22

USING BGCOLOR Use with the. E.g., document.bgcolor = color; If you re using a variable E.g., document.bgcolor = red ; If you re just using a colour Needs to be the last line in your block of code with document.write() commands otherwise the document.write will write over the document.bgcolor command E.g, document.write("<h1>game over, too many guesses!</h1>"); document.write("the number was "+number); document.bgcolor="red"; 23

24 DEBUGGING JAVASCRIPT

SCRIPT NOT WORKING? Common places to look for errors Did you spell variables correctly? Is your code in order? Are you trying to use variables before they have a value associated with them American English spelling of words like color Are all semi-colons where they need to be? Use syntax highlighting to find errors Did you concatenate your strings properly? + in the right places Proper use of double quotation marks Using the error console in Firefox to find problems with your script Tools Error Console Tip: clear the error console, then force refresh Errors option will give you information about what line of code has the problem 25

26 QUESTIONS?