Introduction: JavaScript Syntax, Part 1 Flashcards

Learn the syntax of JavaScript (26 cards)

1
Q

How do you call a random number in JavaScript to the console?

A

Assign a variable to Math.random();

i.e.

let poop = Math.random());

Then call to the console:

console.log(poop);

This prints a number between 0 and 1. For greater numbers multiply it by any given number.

i.e. console.log(poop)* 50);

The code above gives you a random number between 1 and 50.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you ensure a random number generator(or any number) is a whole number?

A

By using the math.floor function.

math.floor(math.random()*50);

This statement generates a random number between 0 and 1 then multiplies it by 50 and then using the math floor statement rounds it down to the nearest whole number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What statement prints to the terminal?

A

console.log()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are functions such as Math.random and Math.floor case sensitive?

A

YES! Look at case as a single error in punctuation can cause your program to fail.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does the Math.ceil() method do?

A

Math.ceil() Rounds up to the nearest whole number.

i. e.
console. log(Math.ceil(43.8));

The statement above evaluates and displays to the terminal the whole number 44.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you check to see if a number is an integer and display the outcome to the terminal?

A

console.log(Number.isInteger());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how do you comment out a single line?

A

By using the // marks

// this is a comment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you comment out multiple lines?

A

By using the /* marks.

/*I am a super large comment

booga boo /*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Different data types such as numbers, strings, etc. can have properties and data stored. What are they denoted by?

A

By a period sign .

i.e.
Number.isInteger
Hello.length
Math.random
Math.floor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare a variable in JavaScript?

A

By using var.

i.e.
var poop= 'stinkers'

in the statement above I have declared a variable named poop and initialized a “value” of the string ‘stinkers’ in it.

“HINT”

You can also use let and const for variables(More on that later)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the += operator do?

A

The += operator will first add the number to the value and then set the value to the new sum.

i.e.

let w = 4

w += 1;

The resulting statement is equal to 5. First we added 1 to the original value of w and then assigned the new sum so therefore the new value of w is now 5 instead of 4.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Operator Review:

What do the following Operators do?

\+=
-=
*=
/=
--
\++

use them in a statement, Check your answers.

A

+= will add the sum and set the sum as the new value of a variable.

likewise the -= will do the same thing except subtract.

the same works with division and multiplication as well.

i.e.

let poop = 2;

poop += 1;

Console.log(poop)

The statement above will display poop as equalling 3 instead of two.

++ and – will increment or decrement your number.

let poop = 2;

poop – 1;

console.log(poop);

this statement will decrement poop to a value of 1 instead of two.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you concatenate strings?

A

By using a + simple.

i.e.

if I have a variable named favoriteAnimal that equals dog. I can use it in a sentence like this:

console.log(‘my favorite animal is’ + favoriteAnimal + ‘.’);

the resulting sentence is my favorite animal is dog.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you interpolate strings?

A

By using the ` button. This is located to the left of the 1 key. doing so allows you to not worry about entering or exiting ‘ ‘ or “ “.

the format is as follows:

console.log(TEXT HERE ${variableName} more text.)

The statement above allows you to add as much or as little text as needed without having to concatenate for easier readability.

REMEMBER it uses ` at the beginning and ` at the end. To interpolate your variable text its ${variableName}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does typeof do?

A

typeof will tell you the type of variable it is by displaying to the console.

i.e.

let poop = ‘crap’;

console.log(typeof poop);

the terminal will output string in response

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Convert 750 Kelvin to Celsius and Fahrenheit. These numbers do not change.

Use these equations:

Fahrenheit = Celsius * (9/5) + 32

Celsius is 273 less than Kelvin

A

const kelvin = 750;

const celsius = kelvin - 273;

const fahrenheit = Math.floor( celsius *(9/5) + 32;

console.log(fahrenheit);

The result should display: 157

17
Q

How do you use the string method .toLowerCase?

A

//Declare a variable, add the string method to the end:

let myName = ‘KONNER’.toLowerCase;

the result will force the output to all be in lower case.

18
Q

What does an if statement look like? No code completion.

A

if(conditionGoesHere) {
console.log(‘yeah buddy’);
}

i.e.

let poop = true;

if(poop == true) {
console.log(‘time to go’)
}

19
Q

What is the difference between if(sale)… and if(sale === true)…

truthy vs. identity

A

if(sale) determines if a statement evaluates to true(a truthy value)

whereas if(sale === true) it is limited to determining if the sale condition is identical to the boolean data type true.

20
Q

INFORMATION CARD:

Conditional Statements that are comparison statements can be helpful to think that if they evaluate to YES then they are true and if the answer is NO then they are false.

So..

the statement 10< 12 can be phrased:

Is 10 < 12? The answer is YES so therefore the statement is true.

A

Just information

21
Q

What is the identity operator?(===)

A

=== checks to see if the two data types are exactly the same. i.e.

‘apples’===’oranges’ evaluates to false because the strings are not equal.

You are not checking for a comparison, you are checking for a literal equal in its primitive form.

22
Q

write an if else statement?

A

if(urgeToPoop > 8) {
console.log(‘I’m gonna poop my pants!)

} else {
console.log(‘I think I can hold it..);
}

23
Q

What are the three logical operators?

A

The and operator &&
The or operator ||
The not operator !

24
Q

What does the && operator do?

A

checks an if statement to see see if both conditions are true.

ex:

let toiletNear = 'yes';
let buildingPressure = 7;

if(toiletNear === ‘yes’ && buildingPressure >= 7) {

console.log(‘You blew up the toilet.’);
} else {
console.log(‘No poop for you!’);
}

25
What is considered a falsy value?
Falsy values are: 0 empty strings like "" or '' null- which represent when there is no value at all undefined- which respresent a declared variable that lacks a value. NaN, or Not a Number
26
The Ternary Operator Shortening If statements. What is the syntax of them?
isNightTime ? console.log('Turn on the lights!') : console.log('Turn off the lights");