Part 5- A Smarter Way to Learn JavaScript Flashcards

1
Q

If you use the “indexOf()” method and it does not find your term within the text, what does it return?

A

-1

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

How would you program an IF statement to test if a term is found in certain text?

A
  1. assign the index number of a term to a variable, like indexNumber
  2. Use the indexOf method to find the index number of the term
  3. if (indexNumber !=== -1) {
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In an “indexOf()” function, the parentheses hold the term you’re searching for. What sorts of things can go in the parentheses?

A

*a string
*a variable
*a slice statement
*maybe lots of other things?

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

How do you pull a single character from a string?

A

stringName.charAt(x)
where x = the index number

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

What is one drawback of charAt(x)?

A

It doesn’t let you change the character. It just lets you find it.

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

How do I use charAt to find the last digit of a particular string when I don’t know how long the string is?

A

varName.charAt(str1.length - 1)

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

What’s the easiest way to replace part of a string with something else?

A

replace()

string.replace(“old text”, “new text”);

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

The basic replace method replaces how many instances?

A

Just the first one.
To get it to replace everything, you have to put “//g” around it like this: /replace me/g

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

In a replace () method, how do you keep the original version in tact?

A

Assign the change to a new variable:
y = x.replace(“a”, “b”);

but x = x.replace(“a”, “b”); will overwrite the original

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

Do you have to set a replace() method to a variable, even the same variable?

A

No. These are the same for the var x
:
alert(x.replace(“up”, “on”));

alert(x= x.replace(“up”, “down”));

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

What does it mean when
indexOf () returns a -1?

A

That the term was not found within that string.

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

How do you round a number to the nearest whole number?

A

Math.round(numX);

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

How do you round a number to the nearest whole number without losing the original number?

A

*set up a new variable

numY = Math.round(numX);

numY becomes the rounded number while the original numX is preserved.

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

How do you round a number down to the nearest integer?

A

Math.floor(12.34);

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

How do you round a number up to the nearest integer?

A

Math.ceil(12.34);

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

What’s the code for generating a random number?

A

Math.random();

17
Q

How long is the number that Math.random() generates?

A

16 digits between zero and one.

18
Q

How would you change a number generated by Math.random() into something usable, like one of the six numbers from a die?

A

Multiply by six, then add 1.

Then round to floor.

*You have to add one because if you don’t, there is no rounding function that gets all generated digits to the numbers 1-6

19
Q

Code for converting a string to an integer? (whole number)

A

parseInt(“165”);

20
Q

Code for converting a string to a decimal number?

A

parseFloat(“165”);