App Academy Prep Flashcards
Add, subtract, multiply, and divide numbers
3 + 3 == 6
3 * 4 == 12
4 - 3 == 1
8 / 2 == 4
create variable, assign it 3
my_favorite_number = 3
# recall my_favorite_number, use it in an expression, save it # to a variable
my_favorite_square = my_favorite_number * my_favorite_number
# block of code that will take in a number, and return its # square
def square(num) # squares num; the last expression in a method gets returned # implicitly num * num end
square(3) == 9
# search for `target_item` in `items`; return the array index # where it first occurs
def find_item(items, target_item) i = 0 while i < items.count current_item = items[i] if current_item == target_item # found item; return its index; stop here and exit the # method return i end i += 1 end
return nil to mean item not found
nil
end
Use if, else, elsif to describe negative, small, and big numbers
def how_big(num) if num < 0 "negative" elsif num < 10 "pretty small" elsif num < 100 "not too big" elsif num < 1000 "plenty big" else "way too big" end end
What is an array?
Arrays are lists of items.
How do you create an empty array
empty_array = []
Create an array with some_prime numbers
some_primes = [2, 3, 5, 7]
some_primes = [2, 3, 5, 7]
fetch 2 and 3 from this array
some_primes[0] == 2 # first prime
some_primes[1] == 3 # second prime
Set the first element of the array to 2
some_primes[0] = 2 # set the first element of the array to 2
Add an element to the end of an array
some_primes «_space;11 # add the next prime to the array
get the number of items in an array
[2, 3, 5, 7].count == 4
What is a while loop?
A while loop repeatedly executes a block as long as its “condition” is true