AP CSP Q1 T2 Codehs.org
Initialize variable
var numApples = 5
What will the following code print to the screen? println(2 + 2); 2 + 2 4 22 Nothing
4
What function do you need to call to ask the user of the program to enter text? readLine readln text println
readLine
Ways to get user input
readLine (prompt) readInt (prompt) readFloat (prompt)
What keyword do you need to use to define a variable in JavaScript? variable var int x
var
Bad variables
- Has a space: var my var; -Starts with a number: var 8ballLocation; -Not descriptive: var n;
Basic Types of Variables
- Numbers - Boolean - String
Numeric Types of Variables
-Integers: whole numbers and opposites used to count: var numApples = 5; -Floats: like real numbers, can have decimals: var cost = 40.25;
How do you write a conversion factor in javascript?
All caps with underscore between words var DOLLARS_TO_POUNDS = 0.6462
To ask the user of the program for a number, which function should you use? readLine readInt readFloat Both readInt and readFloat are for numbers.
Both readInt and readFloat are for numbers
Naming variables
Must start with a letter, or $(dollar sign) or _ (underscore) Rest of name can have letters, number or _ Variables name must be written in "lower camel case"
String Types of Variables
String (text): strings go between quotes var name = "Jeremy"; var question = "How are you?";
In the following code, we create a circle and add it to the screen. What is the meaning of the x variable? var x = 20; var circle = new Circle(x); add(circle); The color of the circle The diameter of the circle The radius of the circle The position of the circle
The radius of the circle
Boolean Types of Variables
True or false var loggedIn= false; var grameOver= true; Have to have exact capitalization here
How do you read floats from the user?
Use readFloat var coast = readFloat("Enter cost: "); println (cost);
How do you read integers from the user?
Use readInt var age = readInt ("Enter age:"); println (age);
How do you read strings from the user?
Use readLine var name = readLine ("Enter name: "); ->this is to ask println (name);
To carry out an arithmetic procedure
function start(){ var and readInt var and readInt var procedure = var +/*- var println(procedure); }
Graphics make a blue circle named blue on canvas radius =30 and center at (100,50)
function start(){ var blue = newCircle(30); blue.setPosition(100,50); blue.setColor(Color.blue); add(blue); }
Write out example to find out sum of two integers
function start(){ var first = readInt("First:"); var second = readInt("Second:"); var sum = first + second; println(sum); }
Graphics make a red rectangle named rect with width 100 and height 50 at position (60,150)
function start(){ var rect = new Rectangle(100,50); rect.setPosition(60,150); -> top left corner rect.setColor(Color.red); add(rect); }
Graphics "Hello world" on canvas
function start(){ var text = new Text ("Hello world"); text.setPosition(300,200);-> bottom left corner text.setFont("50 pt Arial"); add(text); }
What are the coordinates of the top right corner of the screen? 0, 0 0, getWidth() getWidth(), 0 getWidth(), getHeight()
getWidth(), 0
What function do you need to call to get the width of the screen? width() getWidth() screenWidth() getScreenWidth()
getWidth();
To change variable of previous example
numApples = 0
What is the proper function to call to print to the screen? write println PRINT post
println
How do you print "Hello world."
println("Hello world.");