Unit 3: Programming
What is a JavaScript example that converts a string to all lowercase or uppercase letters?
"HI".toLowerCase(), "hi".toUpperCase()
What is a JavaScript example that reports the length of a string?
"alphabet".length
What is a JavaScript example that finds the position of a character in a string?
"hello".indexOf("e")
Which of these are valid lines of JavaScript to store a string? Choose all answers that apply: (Choice A) var age = "35"; (Choice B) var greeting = "Ahoy, matey; (Choice C) var nickname = Polly"; (Choice D) var coupon = "$5 off all cheese!"; (Choice E) var empty = ""; (Choice F) var store = BotsRUs; (Choice G) var title = "I, Robot";
(Choice A) var age = "35"; (Choice D) var coupon = "$5 off all cheese!"; (Choice E) var empty = ""; (Choice G) var title = "I, Robot";
What is pseudocode?
A language that doesn't actually run anywhere, but still represents programming concepts that are common across programming languages.
What is this line of code called? ... var score = 0;
A statement
What is this line of code called? println("Hello, World");
A statement.
Definition: Combines two conditions, evaluating to true only if both conditions are true.
AND Operator
Example: if (cumulativeGPA > 2.0 && totalUnits >= 120) { println("You can graduate!"); }
AND Operator
Usage: Condenses nested conditionals, enhancing code efficiency and clarity.
AND Operator
What needs to be done to fix this code? var name = "Winston"; var title = "Sir"; var greeting = "Hello, " + title + name; println(greeting);
Add "+" " between title and + name; var greeting = "Hello, " + title +" " + name;
What is a statement?
An instruction to the computer about something we need it to do.
What can a string contain?
Any number of characters, including none at all, and those characters can be letters, numbers, or symbols.
Addition (+), subtraction (-), multiplication (*), division (/), and remainder (%) are what?
Arithmetic operators (Mathematical expressions)
an expression that evaluates to either true or false.
Boolean expression
Computer programs also make decisions, using __________ _____________ (true/false) inside ___________ (if/else).
Boolean expressions, conditionals
Math.abs() is an example of what?
Built-in procedure.
What is the pseudocode equivalent of this JavaScript? "Ghost" + "busters"
CONCAT ("Ghost", "busters")
What is the pseudocode equivalent of this JavaScript? "San " + "Diego"
CONCAT ("San ", "Diego")
Definition: Crafting logically equivalent statements to enhance understanding. Example: Refactoring a complex statement like "You cannot go to the park if there is NOT more than one parent home AND if your sister is NOT asleep OR NOT at least one parent is home" to "You can go to the park if both parents are home OR if your sister is asleep AND at least one parent is home."
Clarity in Expression
Usage: Improves readability, comprehension, and maintainability of rules and logic, both in life rules and programming.
Clarity in Expression
What does println() represent?
Code that adds a new line and visualizes what is inputted.
What does DISPLAY () represent?
Code that adds a space and visualizes what is inputted.
Definition: Utilizes logical operators (AND, OR, NOT) to implement complex logic.
Combining Operators
Example: if (biologyGrade > 65 && (chemistryGrade > 65 || physicsGrade > 65)) { println("science requirement satisfied"); }
Combining Operators
Usage: Enables concise representation of multifaceted conditions, facilitating comprehensive program logic.
Combining Operators
Definition: Combinations of logical expressions to determine conditions for decisions. Example: "You cannot go to the park if your sister is awake OR your room is dirty."
Compound Statements
Usage: Allows handling of more complex scenarios and decision-making processes by integrating multiple conditions.
Compound Statements
This can be seen as what type of nesting? var level; if (rating < 5000) { level = "mild"; } else { if (rating < 20000) { level = "medium"; } else { if (rating < 70000) { level = "hot"; } else { level = "extreme"; } } }
Deeper nesting
What are the results of this Boolean expression in a variable? var belowFreezing = temperature < 32;
Depending on the current value of temperature, the belowFreezing variable will now store either true or false.
What does this line of pseudocode mean: DISPLAY ("Howdy") DISPLAY ("Neighbor!")
Displays the value of Howdy followed by a space and the value of Neighbor followed by another space.
What does this operator mean? ==
Equality
What are syntax?
Errors in the use of the language rules. Ex. variable names spelt incorrect and variables not being declared before use.
What do alternate nesting flows entail?
For most nested conditionals, there are many possibilities for the order of the various conditions.
What does this operator mean? >
Greater than
What does this operator mean? >=
Greater than or equal
What result is displayed for this code? var name = "Winston"; var title = "Sir"; var greeting = "Hello, " + title + name; println(greeting);
Hello, SirWinston
Numbers, strings, lists, dictionaries, and Boolean values (true/false) are mostly stored where?
In variables
What does this operator mean? !=
Inequality
What does constant mean regarding computer science?
It can not be re-assigned.
What does this operator mean? <
Less than
What does this operator mean? <=
Less than or equal
Definition: Multiple expressions having the same meaning but different forms. Example: "You cannot go to the park if your sister is awake" is logically equivalent to "You can go to the park if your sister is asleep."
Logical Equivalence
Usage: Facilitates clearer expression of conditions and decisions in both everyday life and computer science.
Logical Equivalence
Definition: Elements such as NOT, AND, and OR used to manipulate logical expressions. Example: "You can go to the park if your sister is NOT awake AND your room is NOT dirty."
Logical Operators
Usage: Enables precise specification of conditions and criteria for decision-making.
Logical Operators
Definition: Basic representations of logical operators in pseudocode
Logical Operators in Pseudocode
Example: IF (cspGrade ≥ 75 OR progGrade ≥ 75) { DISPLAY("You're eligible for AP CS A!") } IF (cumulativeGPA > 2.0 AND totalUnits ≥ 120) { DISPLAY("You can graduate!") } IF (NOT(cumulativeGPA > 2.0 AND totalUnits ≥ 120)) { sendEmail(); }
Logical Operators in Pseudocode
Usage: Offers a simplified conceptualization of logical operations for easier understanding and implementation.
Logical Operators in Pseudocode
(77 - 32) * 5/9 This expression is an example of what?
Multiple operators
Definition: Reverses the truth value of an expression.
NOT Operator
Example: if (!(cumulativeGPA > 2.0 && unitsCompleted >= 120)) { sendEmail(); }
NOT Operator
Usage: Offers an alternative to nested conditionals or negating individual conditions.
NOT Operator
What is concatenation?
Nothing more than the joining of two or more strings
Combines two conditions, evaluating to true if either condition is true.
OR Operator
Example: if (cspGrade >= 75 || progGrade >= 75) { println("You're eligible for AP CS A!"); }
OR Operator
Usage: Shortens code, simplifies maintenance, and improves readability.
OR Operator
Addition (a + b), subtraction (a - b), multiplication (a * b), division (/ b), and remainder (a MOD b) are what?
Pseudocode for mathematical expressions.
What does Math.pow(base, exp) do?
Returns base to the power of exp.
What does the procedure do? Math.round(num)
Returns num rounded to the nearest integer.
What does the procedure do? Math.cos(num)
Returns the cosine of num, an angle specified in radians.
What does the procedure do? Math.floor(num)
Returns the largest integer less than or equal to num.
What does the procedure do? Math.max(x, y, z...)
Returns the maximum of the given numbers.
What does Math.min(x, y, z...) mean?
Returns the minimum of the given numbers.
What does the procedure do? Math.sin(num)
Returns the sine of num, an angle specified in radians.
What does the procedure do? Math.ceil(num)
Returns the smallest integer greater than or equal to num.
What does the procedure do? Math.tan(num)
Returns the tangent of num, an angle specified in radians.
What is the pseudocode equivalent of this JavaScript? "97302-3679".substr(6, 4)
SLICE ("97302-3679", 7, 4)
What is the pseudocode equivalent of this JavaScript? "San Diego".substr(0, 3)
SLICE ("San Diego", 1, 3)
What does the SINGLE parameter do in this command: println()
Specifies what text the computer should display.
All programs are made up of _______(s), and each _______ is an instruction to the computer about something we need it to do.
Statement
What does this operator mean? ===
Strict equality
What does this operator mean? !==
Strict inequality
var name = "Winston"; var title = "Sir"; var greeting = "Hello, " + title + name; Notice we're concatenating quoted strings with variables. What are the quoted strings called?
String literals
We can also do the opposite of concatenation: we can slice a string and just store that slice. We call that slice a ________.
Substring
What does this pseudocode mean? difficulty ← "medium"
The variable difficulty is storing the string "medium".
What are variables?
Where a program can store data. Has a name, a value, and a type. The value might change over time.
In pseudocode, constants are written in ___ ____, like the constant PI.
all caps
This option is called what? var upperStreet = address.toUpperCase().substr(4, 9);
chain operations.
What does this number sign checker contain? var numberSign; if (number > 0) { numberSign = "positive"; } else if (number < 0) { numberSign = "negative"; } else { numberSign = "neutral"; }
chained conditionals
In programming lingo, we call each letter a _________ and we call the sequence a ______.
character; string
If we want our programs to execute a different set of instructions when the condition is false, then we can use an ____ statement.
else
What is the pseudocode equivalent of this JavaScript? var hair = "curly";
hair ← "curly"
Computers run each statement ___ ______.
in order
This pseudocode shows a simple ________ conditional, with one outer __/____ and one inner __/____: IF (<condition 1>) { <instructions 1> } ELSE { IF (<condition 2>) { <instructions 2> } ELSE { <instructions 3> } }
nested; IF, ELSE; IF, ELSE
Computer programs use conditionals to select the correct path for a program to go down. When a program only selects one of two paths, it can use a simple conditional (if/else). When a program selects one of many paths, it can use ______ or _______ conditionals.
nested; chained
An ___-__-___ error is a common kind of logic error that can happen when you're dealing with indices and accidentally use too high or low an index.
off-by-one
What is the pseudocode equivalent of this JavaScript? var password = "2278";
password ← "2278"
What is the line of code to display the value in PSEUDOCODE.
println()
The println() command is also called a function, method, or __________
procedure
Is this string pseudocode or JavaScript. a ← "STRING"
pseudocode.
var score = 0; var lives = 3; What code re-assigns the variable of score to a new value of 5. (JavaScript)
score = 5;
One way to slice in JavaScript is with the _________ command. It takes two parameters, start position and length.
substr()
What is programming?
the act of designing and implementing computer programs (set of instructions)
What does this pseudocode mean? age ← 21
the variable age is assigned the value 21
1000 * 60 * 60 How would you write the statement to store the results of this mathematical expressions in a variable called hourMS? (JavaScript)
var hourMS = 1000 * 60 * 60;
var numLoaves = 2; How would you write the statement to use this variable inside mathematical expressions of a variable called totalFlourCups where you want 4 times the numLoaves? (JavaScript)
var numLoaves = 2; var totalFlourCups = numLoaves * 4;
What's the pseudocode equivalent of this JavaScript? var x = 200;
x ← 200
What's the pseudocode equivalent of this JavaScript? var y = -44;
y ← -44