Numbers n' Strings

  • So what about numbers? We know how to store words but let's try the same thing with some numbers.

    Declare and set a variable named number to the value 42. Numbers, unlike strings, shouldn't be surrounded by quotes.

    Show hint

    To declare and set a variable in a single line, you use var variableName = variableValue, where variableName is the name of the variable, and variableValue is the value you want to assign to the variable.

    For this exercise, the variable name is number and the value is 42. Thus, the answer should be var number = 42 - remember not to use quotes!

  • 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.

    Show hint

    You can use the variable number the same way you use a number. Just enter number / 2 and press enter.

  • 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 % 10 and press enter. Try it!

    Show hint

    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.

  • 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 myString in order to pass this one. Remember as well that it's case-sensitive - make hello all lowercase!

    Show hint

    Remember that declaring and setting a variable means using the syntax var myVar = "yourVarValue"; and pressing enter. hello should be entered all lowercase as well.

  • 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.

    Show hint

    To pass this lesson, just follow the instructions and type "hello".substring(0,2) and then press enter.

  • 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.

    1. Use var to declare a variable called three and type in the equals sign = after the variable name three.
    2. 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.

    Show hint

    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);

  • 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 keyword replace, 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".

    Show hint

    Remember, the proper syntax here is to use "original text".replace("text to be replaced", "new text");

  • 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.

    Show hint

    To make something all uppercase, just type .toUpperCase() after it. Don't forget that the case matters!

Keyboard shortcuts: Run CTRL + Enter Reset CTRL + S