Part 5- A Smarter Way to Learn JavaScript Flashcards
If you use the “indexOf()” method and it does not find your term within the text, what does it return?
-1
How would you program an IF statement to test if a term is found in certain text?
- assign the index number of a term to a variable, like indexNumber
- Use the indexOf method to find the index number of the term
- if (indexNumber !=== -1) {
In an “indexOf()” function, the parentheses hold the term you’re searching for. What sorts of things can go in the parentheses?
*a string
*a variable
*a slice statement
*maybe lots of other things?
How do you pull a single character from a string?
stringName.charAt(x)
where x = the index number
What is one drawback of charAt(x)?
It doesn’t let you change the character. It just lets you find it.
How do I use charAt to find the last digit of a particular string when I don’t know how long the string is?
varName.charAt(str1.length - 1)
What’s the easiest way to replace part of a string with something else?
replace()
string.replace(“old text”, “new text”);
The basic replace method replaces how many instances?
Just the first one.
To get it to replace everything, you have to put “//g” around it like this: /replace me/g
In a replace () method, how do you keep the original version in tact?
Assign the change to a new variable:
y = x.replace(“a”, “b”);
but x = x.replace(“a”, “b”); will overwrite the original
Do you have to set a replace() method to a variable, even the same variable?
No. These are the same for the var x
:
alert(x.replace(“up”, “on”));
alert(x= x.replace(“up”, “down”));
What does it mean when
indexOf () returns a -1?
That the term was not found within that string.
How do you round a number to the nearest whole number?
Math.round(numX);
How do you round a number to the nearest whole number without losing the original number?
*set up a new variable
numY = Math.round(numX);
numY becomes the rounded number while the original numX is preserved.
How do you round a number down to the nearest integer?
Math.floor(12.34);
How do you round a number up to the nearest integer?
Math.ceil(12.34);