CS 105: Introduction to Computer Programming (using JavaScript) Quiz: Khan Academy: Intro to While Loop

Similar documents
CS 105 Introduction to Computer Programming in JavaScript Midterm Exam Review

krl gl drlrpcj cpr ougei tm arjgrvr tfct wfgef tfry wgqf tm ar tpur.

Clearly print (not sign) your name:

nt (not sign) y z = 2; z = z + 1; var University of New Mexico Fall 2016 your answer. answers on the exam. graded as you choose you choose to turn in

Real Time Data Plotting

Khan Academy JavaScript Study Guide

Iteration in Programming

Clearly print (not sign) your name:

CS-257L Nonimperative Programming: Scheme!

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

Graphics. HCID 520 User Interface Software & Technology

PROBLEM SOLVING AND PROGRAM. Looping statements Executing steps many times

Graphics. HCID 520 User Interface Software & Technology

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Cell Phone. Adobe Illustrator Lesson 16 Working with Gradients Transparency Menu 1. START>PROGRAMS>ADOBE ILLUSTRATOR CS3

Questions form Chapter 3 and Some Optimization Tips for C++ Assignment 2: Antialiasing

CISC 1600 Lecture 3.1 Introduction to Processing

7 Control Structures, Logical Statements

CS 101 Randomness. Lecture 21

Web Programming 1 Packet #5: Canvas and JavaScript

Computer Graphics - Treasure Hunter

This is an open-book, open-notes, open-computer exam. You may not consult with anyone other than the instructor while working on this exam.

Lecture 15. For-Loops

easel Manager Danielle Crosswell dac2182 Language Guru Tyrus Cukavac thc2125 System Architect Yuan-Chao Chou yc3211 Tester Xiaofei Chen xc2364

More About Factoring Trinomials

CPSC / Scan Conversion

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

CS 351: Design of Large Programs.

function makeline(length, ch){ var str = ""; if (length >= 3)

CS 351: Perspective Viewing

Kimberly Nguyen Professor Oliehoek Introduction to Programming 8 September 2013

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

The counting numbers or natural numbers are the same as the whole numbers, except they do not include zero.,

University of Cincinnati. P5.JS: Getting Started. p5.js

Computer Programming

To specify the dimensions of the drawing canvas use the size statement: ! size( 300, 400 );

1. Complete these exercises to practice creating user functions in small sketches.

Python lab session 1

1-3 Square Roots. Warm Up Lesson Presentation Lesson Quiz. Holt Algebra 2 2

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Programming Iterative Loops. for while

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

CISC 1600, Lab 2.1: Processing

Practice Written Examination, Fall 2016 Roger B. Dannenberg, instructor

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

1. More jquery Methods 2. JavaScript + SVG: Raphaël 3. About SVG 4. Working with SVG 5. Animating SVG

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

1. Variables, Again 2. JavaScript Operators Types 3. Iteration: the for-loop 4. Working with Strings 5. Making Decisions 6.

A B C D CS105 03a Interaction

Forms, Form Events, and Validation. Bok, Jong Soon

Control Flow: Loop Statements

CMPT 120 Introduction To Computing Science And Programming I. Pseudocode. Summer 2012 Instructor: Hassan Khosravi

Create Text Inside a Shape with Photoshop CS6

(a) Assume that in a certain country, tax is payable at the following rates:

Graphics (Output) Primitives. Chapters 3 & 4

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Clearly print (not sign) your name:

CSE120 Wi18 Final Review

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Midterm #1a Fall minutes

Design Programming DECO2011

Data Visualization (CIS 468)

CS-259 Computer Programming Fundamentals Arrays

Data Visualization (DSC 530/CIS )

CISC 1600, Lab 2.3: Processing animation, objects, and arrays

Watch the following for more announcements

Let's see a couple of examples

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Conditionals !

GIMP TEXT EFFECTS. Text Effects: Outline Completed Project

Late Penalty: Late assignments will not be accepted.

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Chapter 17. Fundamental Concepts Expressed in JavaScript

Day 1: Introduction to MATLAB and Colorizing Images CURIE Academy 2015: Computational Photography Sign-Off Sheet

CS 101 Computer Science I Fall Instructor Muller. stddraw API. (DRAFT of 1/15/2013)

(SSOL) Simple Shape Oriented Language

CS 61B Data Structures and Programming Methodology. June David Sun

Theory of control structures

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Data Representation From 0s and 1s to images CPSC 101

Fireworks 3 Animation and Rollovers

Unit 7: Algorithms and Python CS 101, Fall 2018

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Topic Notes: Java and Objectdraw Basics

IMAGE & TEXT. Workshop 5

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Practice Midterm 1. Problem Points Score TOTAL 50

WEB GRAPHICS ADOBE PHOTOSHOP CS3. Learning Outcomes:

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Data Visualization (DSC 530/CIS )

CS 351 Design of Large Programs Java and Socket Communication

CS1100 Introduction to Programming

UCT Algorithm Circle: Number Theory

Unit 10: Data Structures CS 101, Fall 2018

Transcription:

CS 05: Introduction to Computer Programming (using JavaScript) Loops Instructor: Joel Castellanos e-mail: joel@unm.edu Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 39 0/8/207 Quiz: Khan Academy: Intro to While Loop What does this JavaScript program display on the canvas? fill(0, 0, 0); var message = "y is now "; var y = 0; while(y < 60) { text(message + y, 30, y); y += 5; } 2 a) y is now 0 d) y is now 0 b) y is now 0 25 40 55 y is now 25 y is now 40 c) y is now 30 0 30 25 30 40 30 55 y is now 55

Print Multiples of 3 from 3 to 30 ) fill(0,0,0); 2) 3) text( 3, 0, 0); 4) text( 6, 0, 0); 5) text( 9, 0, 0); 6) text(2, 0, 0); 7) text(5, 0, 0); 8) text(8, 0, 0); 9) text(2, 0, 0); 0)text(24, 0, 0); )text(27, 0, 0); 2)text(30, 0, 0); Canvas: 3 Print Multiples of 3 from 3 to 30 ) fill(0, 0, 0); 2) 3) text( 3, 0, 0); 4) text( 6, 20, 0); 5) text( 9, 30, 0); 6) text(2, 40, 0); 7) text(5, 50, 0); 8) text(8, 60, 0); 9) text(2, 70, 0); 0)text(24, 80, 0); )text(27, 90, 0); 2)text(30,00, 0); 4 Canvas: 2

Print Multiples of 3 from 3 to 30 ) fill(0, 0, 0); 2) 3) text( 3, 0, 0); 4) text( 6, 20, 0); 5) text( 9, 40, 0); 6) text(2, 60, 0); 7) text(5, 80, 0); 8) text(8, 00, 0); 9) text(2, 20, 0); 0)text(24, 40, 0); )text(27, 60, 0); 2)text(30, 80, 0); ) fill(0, 0, 0); 2) 3) var x = 0; 4) var n = 3; 5) while(n <= 30) 6) { 7) text(n, x, 0); 8) n += 3; 9) x += 20; 0) } 5 Canvas: 3 6 9 2 5 8 2 24 27 30 Quiz: What is displayed on the Canvas? 6 textsize(30); fill(0,0,0); var message = "y="; var y = 00; while(y < 200) { text(message + y, 30, y); y += 25; } (c) (a) y=25 y=50 y=75 (b) y=00 y=25 y=50 y=75 y=00 y=25 y=50 y=75 y=200 (d) y=00, 30, 00 y=25, 30, 25 y=50, 30, 50 y=75, 30, 75 y=200, 30, 200 3

Print the Factors of an Integer Examples: num= 0 : Factors:, 0, 2, 5 num = 2: Factors: 6, 2, 2,, 3, 4 Algorithm: 7 var i =. Loop while i is less than or equal to num. if i divides evenly into num then print i. Add to i. Modulus Operator ) fill(0, 0, 0); 2) text( 7 % 3, 0, 0); 3) text( 8 % 3, 0, 30); 4) text( 9 % 3, 0, 50); 5) text(0 % 3, 0, 70); 2 0 8 4

Quiz: What is displayed on the Canvas? ) fill(0, 0, 0); 2) text( 22 % 5, 0, 0); 9 (a) (b) (c) (d) (e) 2 4 4.4 4 0 0 4 5 0 0 Quiz: What is displayed on the Canvas? ) var brenda = ; 2) fill(0, 0, 0); 3) while (brenda < 5) 4) { 5) text( 7 % brenda, 0, 5*brenda); 6) brenda += ; 7) } 0 (a) (b) (c) (d) (e) 7 0 3 2 3 4 0 2 3 7 5

Print the Factors of an Integer ) fill(0, 0, 0); 2) var y = 0; 3) var num = 50; 4) var i = ; 5) while (i <= num) 6) { 7) if (num % i === 0) 8) { text(i, 0, y); 9) y += 20; 0) } ) i +=; 2) } Sets font color to black. In JavaScript, % is the modulus operator. It returns the remainder. If a large font, is used, more than 20 will need to be added to y each time a new factor is found and displayed. Getting Input form an HTML Page HTML <body> <h>enter a <i>positive integer</i> and I will display its factors: <input type="text" style="font-size: 00%" id="inputnum" value="00"> </h> 2 JavaScript var inputnum = document.getelementbyid("inputnum"); var num = Number(inputNum.value); 6

Print the Prime Factors of an Integer 3 is NOT a prime number. The first 20 prime numbers are: 2, 3, 5, 7,, 3, 7, 9, 23, 29, 3, 37, 4, 43, 47, 53, 59, 6, 67, 7. Examples: num= 0 : Prime Factors: 2, 5 num = 2: Prime Factors: 2, 2, 3 Algorithm: Why? var prime = 2. Loop while prime <= the square root of num: if prime divides evenly into num then Print prime. Change num to equal num divided by prime. if prime does NOT divide evenly into num then add to prime. Why will this work? For example, if num = 6, then 4, which divides 6, yet is not a prime, should not (will not) get printed. Print the Factors of an Integer 4 ) var y = 0; 2) var num = 50; 3) var prime = 2; 4) while (prime <= sqrt(num)) 5) { 6) if (num % prime === 0) 7) { text(prime, 0, y); 8) y += 30; 9) num = num / prime; 0) } ) else 2) { prime +=; 3) } 4) } 5) if (num > ) text(num, 0, y); Note that each time a prime factor is found and printed (in line 7), prime is factored out of num (line 9: num = num / prime). Thus, each time a factor is found, the upper limit of the while loop gets smaller. Contrariwise, each iteration when the current prime is NOT a factor of the remaining num, prime is increased by. Why is this here? 7

What Pattern Does this Draw? 5 strokeweight(2); stroke(0,200,0); line(0,0, 0, 00); line(0,0, 0, 00); line(0,0, 20, 00); line(0,0, 30, 00); line(0,0, 40, 00); line(0,0, 50, 00); line(0,0, 60, 00); line(0,0, 70, 00); line(0,0, 80, 00); line(0,0,00, 00); line(0,0,0, 00); line(0,0,20, 00); (0,0) (0,00) (20,00) (0,00) Drawing the first 4 lines shows the pattern. (30,00) Use a Loop to Shorten Repeated Code 6 strokeweight(2); stroke(0,200,0); line(0,0, 0, 200); line(0,0, 0, 200); line(0,0, 20, 200); line(0,0, 30, 200); line(0,0, 40, 200); line(0,0, 50, 200); line(0,0, 60, 200); line(0,0, 70, 200); line(0,0, 80, 200); line(0,0,00, 200); line(0,0,0, 200); line(0,0,20, 200); strokeweight(2); stroke(0,200,0); var x = 0; while (x <= 20) { line(0,0, x, 200); x += 0; } 8

Deriving the Code to Draw a Pattern 8) var i = 0; 9) while (i<=200) 0){ line(0, i, i, 200); //x,y,x 2,y 2 ) i += 0; (0,0) 2)} (0,0) (0,20) (x,y ) to (x 2,y 2 ) (0,0) to (0,200) (0,0) to (0,200) (0,20) to (20,200) (20,200) (0,30) to (30,200) (0,200) 7 (0,200) Pole: Internet Access In order to access to the Internet within the United States boarders, the government should: a) Require a national ID (or bio ID) and password be entered. b) Require a card with a unique ID chip be inserted into the computer and remain inserted while access is allowed. c) Hold hosting sites responsible for all content posted on those sites. d) Leave Internet access as it is with pseudo anonymity: casual users can be easily tracked by companies, and on a needto-know basis, the government trace anything they want. 8 e) Enable methods for true anonymity. 9

Quiz: Which Image is Drawn? //Assume the canvas is 200x200 pixels var i = 0; while (i<=200) { line(200, 0, i, 200); //x,y,x 2,y 2 i += 0; } 9 a) b) c) d) Spirograph 20 0

A Closer Look at Spirograph 2 http://www.mathematische-basteleien.de/spirographs.htm