Skip to content

Latest commit

 

History

History
351 lines (230 loc) · 6.87 KB

book1-chap1-quiz.answers.md

File metadata and controls

351 lines (230 loc) · 6.87 KB

Quiz - YDKJS: Up & Going 1/3

Chapter 1: Into Programming

Answer Sheet

Section: Code


1. What's a program?

a set of instructions that tell a computer what to do.

2. What's a computer language?

the rules of valid format and combinations of instructions. aka syntax. Just as the English language.

3. What's a variable?

variables are like simple boxes where you can store any of your stuff in.

4. What's a statement?

a group of words, numbers, and operators that performs a specific task.

example: a = b * 2;

5. From the statement a = b * 2 identify its parts (literals, variables, operators).

variables: a, b

literal values: 2

operators: =, *

6. What's an expression?

an expression is any reference to a variable or value, or a set of variable(s) and value(s) combined with operators.

example: a * b

7. How many expressions can you indentify from the statement a = b * 2;?

4 expressions in total:

  • b is a variable expression

  • 2 is a literal value expression

  • b * 2 is an arithmetic expression of multiplication

  • a = b * 2 is an assignment expression

8. What's the difference between interpreted and compiled code?

interpreted code is read on the fly, from top to bottom, line by line, execution happens immediately.

compiled code is when all the translation to commands happens first, then the program is executed.

9. Is JavaScript interpreted or compiled? Explain why.

the JavaScript engine actually compiles the program on the fly then immediately runs the compiled code.

Section: Try It Yourself


10. Output Exercises.

Consider: var a = 1;

Write the code to:

10.1. print a in the console

console.log(a);

10.2. show a in a popup

alert(a);

11. Input Exercises.

Code challenge:

Ask the user's name with a prompt message "Please, type your username" and store it in a variable username, then log the value in the console.

Solution:

var username = prompt('Please, type your username');
console.log(username);

Section: Operators


12. Complete the sentence:

JavaScript has both u___ and b___ operators, and one special t___ operator

unary, binary, ternary

13. Operators types.

Name the types of operators you know, and give some basic examples.

assignment: =, +=, -=, *=, /=

comparison: ==, !=, ===, !==, >, <, >=, <=

arithmetic: +,-, /, *, ++, --

logical: &&, ||, !

Section: Values & Types


14. Name JavaScript built-in types aka as primitive values.

number, string, boolean

15. What's coercion in JS?

when values are converted to a different type, for example, converting a string to a number, or a number to string.

16. Explain the difference between implicit and explicit coercion in JS. Give examples.

implicit coercion is done automatically

example:

var number = 10;
// number is converted to a string
console.log(number);

explicit coercion is when you want to convert values from one type to another

example:

var number = 10;
// I want to convert number to a string
number = String(number);
// number is a string already, no need to converted
console.log(number)

Section: Code Comments


17. What are the two types of comments in JS? Give examples.

single-line comment:

// this is a single line comment

multiline comment:

/* this is
 a multiline
 comment */
18. Why is it important to comment code?

comments describe the hows and whys of something implemented in code. They help to document things in a way other team members and your future self can understand better your code.

comments are one effective way to write more readable code.

Section Variables


19. Does JavaScript use Static or Weak typing?

weak typing

20. Describe static typing aka type enforcement.

variables can hold only the type they are defined with, for example:

int number = 10

21. Describe weak typing aka dynamic typing.

variables can hold any type, for example:

var a = 10;
a = 'Isaac'
22. Name some conventions to write constants in JS.

constants are placed on top of a program.

constants are capitalized separated with an underscore _, example: TAX_RATE

use keywork const only availble for ES6.

Section: Blocks


23. Is this valid code in JS?
var amount = 100;

{
  amount = amount * 2;
  console.log(amount)
}

yes, it's called general block

Section: Conditionals


24. Write a block of code to validate if a variable number is less than 10 to log one digit, log two digits otherwise.

Solution:

if (number < 10) {
  console.log('one digit');
} else {
  console.log('two digits');
}

Section: Loops


25. Write a block of code to log numbers from 0-9 using while, do-while and for loops.

while solution:

var i = 0;
while(i <= 9) {
  console.log('number ->', i);
  i = i + 1;
};

do-while solution:

var i = 0;
do {
  console.log('number ->', i);
  i = i + 1;
} while(i <= 9)

for solution:

for (var i = 0; i <= 9; i = i + 1) {
  console.log('number ->', i);
}
26. What are the three clauses for a for loop?

initialization clause

conditional test clause

update clause

Section: Functions


27. What's a function?

a named block of code that performs a specific task. It's called (executed) by its name.

28. Write a function sum that receives two numbers as arguments and returns the sum of both.

Solution:

function sum (a, b) {
  return a + b;
}
29. What's scope in JS?

a collection of variables as well as the rules for how those variables are accessed by name.

30. Answer true or false for the following statements:

Consider:

function outer() {
  var a = 1;

  function inner() {
    var b = 2;
  }

  inner();
}

outer();
30.1. Does the inner function have access to the outer function scope?

true

30.2. Does the outer function have access to the inner function scope?

false

Section: Challenges


1.1 Create a function calculateAreaOfACircle that receives radius as parameter. Use a constant PI equal to 3.14159. Avoid the temptation of using the Mathlibrary.

Solution:

// create the constat PI here
const PI = 3.14159;

// create your function here
function calculateAreaOfACircle(r) {
  return PI * (r * r);
}

// Do NOT touch this code.
console.log('Expect area of a circle with radius = "3" to be "28.27431" ->', calculateAreaOfACircle(3) == 28.27431)