- What value is now stored in the variable name?
var isKing = true;
var name = isKing ? ‘Arthur’ : ‘Hank’;
ANSWER
name = ‘Arthur’
# name is Arthur. This short syntax for an if statement is called ternary operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
- What is the difference between == and === in Javascript? ANSWER
# Both are conditional operators checking if both sides are equal. When checking equality
# it is always safer to use === instead of == because the === is also checking the type,
# not just the value
# For instance
'5' == 5 # true
'5' === 5 # false
- Write a function that takes two numbers as arguments and returns the sum of the two numbers in Javascript ANSWER
function addNumbers(a, b) {
return a + b;
};
- Write a function that takes an array as an argument and prints out the numbers in the array that are greater than 5 (for example foo([3,6,1,7]) would print out 6 and 7) in Javascript ANSWER
function numStrings(array) {
for(var i = 0; i < array.length; i++) {
if(array[i] > 5){
console.log(array[i])
}
}
}
numStrings([3,6,1,7]) # prints out 6 7
- Write a for loop that will iterate from 0 to 20. For each iteration, it will check if the current number is even or odd, and report that to the screen in Javascript ANSWER
for(i=0; i =< 20; i++) {
if(i%2 == 0) {
console.log(“even”);
}else{
console.log(“odd”);
}
}
- What does 'this' refer to when used in a Java method?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
- Create an object that has properties with name = "fred" and major="music" and a property that is a function that takes 2 numbers and returns the smallest of the two, or the square of the two if they are equal. ANSWER
var newObj = {
name: "fred",
major: "music",
smallestOrSquare: function(numOne, numTwo) {
if (numOne < numTwo) {
return numOne;
} else if (numOne > numTwo) {
return numTwo;
} else {
return numOne *= numTwo
}
}
}
- What’s the result of executing this code and why?
function test() {
console.log(a);
console.log(foo());
var a = 1;
function foo() {
return 2;
}
}
test();
ANSWER
# First console.log is undefined bc a is not yet defined.
# Second console.log is 2, as it is the return value of the inner foo function.
Ruby
- Write a function that takes two numbers as arguments and returns the sum of the two numbers in Ruby ANSWER
def two_numbers(x,y)
return x + y
end
puts two_numbers(2,3)
# A code block is wrapped in def ... end instead of {..}
# No semicolons
# The last value in a function is automtically returned in Ruby
- Write a function that takes an array as an argument and prints out the numbers in the array that are greater than 5 (for example foo([3,6,1,7]) would print out 6 and 7) in Ruby ANSWER
def another_loop(array) {
array.each do |number|
if number > 5
puts number
end
end
end
another_loop([1,2,3,4,5,6,7,8,9,10])
# No loop but each funtion in Ruby
# if statement in if .. end instead of if {...}
- Write a for loop that will iterate from 0 to 10. For each iteration of the for loop, it will multiply the number by 9 and log the result (e.g. "2 * 9 = 18") in Ruby ANSWER
11.times do |number|
puts number * 9
end
Classes
- Create a Celsius class, that takes the temperature as a parameter. ANSWER
class Celcius
def initialize(temperature)
end
end
- In that same class, define a method that returns the temperature in Fahrenheit. For the conversion we can use the formula temperature*1.8 + 32. Round up the result so it doesn’t contain any decimal values. ANSWER
class Celcius
def initialize(temperature)
@temperature = temperature
end
def fahrenheit
(@temperature * 1.8 + 32).round
end
end
- In that same class, create a to_s method, that returns the Celsius temperature formatted e.g. 16 degrees C ANSWER
class Celcius
def initialize(temperature)
@temperature = temperature
end
def fahrenheit
(@temperature * 1.8 + 32).round
end
def to_s
"#{@temperature} degrees C"
end
end
Git
- Assume that you created a new file in a project. What would you do to start tracking it under git (let's assume that the direktory you are currently in already exists)? ANSWER
$ git add file_name
- In the morning, what would you do to make sure you have the most current git branch on your local machine? ANSWER
$ git pull
Command line
- How would you create a new folder named 'new_folder' in the command line? ANSWER
$ mkdir new_folder
- Assuming you created that folder, how would you access that folder using the command line? ANSWER
$ cd new_folder