So what about numbers? We know how to store words but let's try the same thing with some numbers.
Getting Started with Programming (Section 4 of 8)
Numbers n' Strings
Written by Ryan Bubinski
-
Declare and set a variable named
numberto the value 42. Numbers, unlike strings, shouldn't be surrounded by quotes.To declare and set a variable in a single line, you use
var variableName = variableValue, wherevariableNameis the name of the variable, andvariableValueis the value you want to assign to the variable.For this exercise, the variable name is
numberand the value is42. Thus, the answer should bevar number = 42- remember not to use quotes!Rate this exercise -
Let's check out some of the cool things you can do with numbers. You can do all sorts of math, like division.
Let's divide the variable number by 2. Use the / symbol to divide.
You can use the variable number the same way you use a number. Just enter number / 2 and press enter.
Rate this exercise -
You can also find out the remainder of division using the % symbol, known as the modulus operator.
This differs from the
/symbol which does ordinary division and outputs the answer. The%symbol also does division, but just outputs the remainder.For instance, to figure out the remainder when 42 is divided by 10, you can type in
number % 10and press enter. Try it!Use what you learned before and treat number as you'd treat a regular number. As we said above, make sure to use modulus to divide by 10 and find the remainder.
Rate this exercise -
Most of the example variables included in the previous lesson were strings of text. Any text should be entered as a String, a data type. All strings are surrounded by quotes.
Let's create a variable called myString and fill it with the text "hello";
Make sure your variable is called
myStringin order to pass this one. Remember as well that it's case-sensitive - makehelloall lowercase!Remember that declaring and setting a variable means using the syntax
var myVar = "yourVarValue";and pressing enter.helloshould be entered all lowercase as well.Rate this exercise -
Let's see if we can cut out part of a string. For instance, we might want to extract "he" from the string "hello". "he" is called a substring of "hello". To get a substring, we can use the substring() method on a string.
Think of there being a marker to the left of each character. To select for the "he" in "hello", you're starting at position 0 (to the left of the "h") and ending at position 2 (to the left of the "l").
To get the substring
he, you can type "hello".substring(0,2); Try that now.To pass this lesson, just follow the instructions and type
"hello".substring(0,2)and then press enter.Rate this exercise -
We've just seen how to get the first two letters (substring) from the word "hello". Let's take the process of getting substrings one step further. Let's save the first three letters of your name to a variable called
three.- Use var to declare a variable called
threeand type in the equals sign = after the variable namethree. - After the equals sign, use the substring method on your name. So if your name is "Betty", use the substring method after your name.
If you're confused, the tip here may help with more syntax.
As with the exercise before, you can use the substring(start position, end position) method here.
Here you have to set your declare your variable, set it, mention your name, and call the substring method all on the same line. Here's the example syntax:
var three = "Allison".substring(0,3);Rate this exercise - Use var to declare a variable called
-
You can replace parts of strings with other text. For example, if we had a string "hello world" and wanted to modify it to "goodbye world", we would use the
replace()method. So the code we'd type in would be"hello world".replace("hello","goodbye").The general syntax is
"original string".replace("word to be replaced", "replacement word");
With the keywordreplace, you need to enter the text to be replaced and its replacement.Try using replace on the string
"coding rules"to make it"programming rules".Remember, the proper syntax here is to use
"original text".replace("text to be replaced", "new text");Rate this exercise -
We talked before about how upper and lowercase matter in programming. Case matters when calling methods as well.
We'll explain more about methods in the future.
For now, know that strings have methods called toUpperCase() and toLowerCase() that will make all characters in the string upper or lower case.
Try changing a string to all upper case letters.
To make something all uppercase, just type .toUpperCase() after it. Don't forget that the case matters!
Rate this exercise