Longest Word

node v0.12.18
version: 2.0.1
endpointsharetweet
Before going further, there are two more types you need to know. You have already seen the first one a few times: the Number type. Here are a few numbers.
var pi = 3.14; var ten = 10; var negativeTwo = -2;
You can use all the basic arithmetic operators on numbers.
var negativeTwenty = negativeTwo * ten; // here we reference the variables we defined
var negativeTwenty = -2 * 10; // this works too
Numbers also have operators increment and decrement them.
var eleven = ++ten // note that "ten" was defined as a variable above ^
var twelve = 13; --twelve;
// eleven is now set to 11, but what is the current value of the ten variable? console.log(ten);
What happened here? We incremented `ten`, then set `eleven` to that new value. But that changed `ten` in the process. So always keep in mind that the ++ and -- operators change the underlying variables they operate on.
The other (very important) type is the Boolean. These variables can be either true or false, and nothing else.
var theSkyIsBlue = true;
var untrue = false;
Booleans often come about as the result of operations on other types of variables. For example, the result of comparing two numbers:
pi < ten;
10 === 10; // note the triple equals. // single equals would try to set some variable to 10 (and be an error) // double equals would also work in this case, but it is best avoided in javascript
Aside: Why avoid using double equals? It can lead to counterintuitive results in some cases because it tries to convert both operands to the same type before comparing them. Imagine you were comparing the number 10 to the string "10" (which is definitely not the same thing). Double equals would say that the two are equivalent. Triple equals gives us the result we expect.
10 == "10";
10 === "10";
This behavior with double equals gets dangerous when dealing with zero, which can be interpreted as the boolean false.
0 == false;
0 === false;
So stick with triple equals when comparing two values in javascript. Boolean values have operators much like numbers do.
// NOT operator inverts the boolean !true
// AND operator is true if both operands are true var itIsRaining = false; var itIsSunny = true; itIsRaining && itIsSunny; // always false
// OR operator is true if just one operand is true itIsRaining || itIsSunny;
5 === 5.0 && itIsSunny;
Booleans are especially useful in conditional statements. For example:
var goingToPlayVolleyball = true; if (itIsRaining) { goingToPlayVolleyball = false; } console.log(goingToPlayVolleyball); // true because itIsRaining === false;
Sometimes you want to determine some behavior in the case when your conditional fails to be true. You use the `else` keyword for that.
var canPlayVolleyball = true; var canUseUmbrella = true; if (itIsRaining) { console.log('it is raining'); canPlayVolleyball = false; } else { console.log('it is not raining'); canUseUmbrella = false; }
Finally, loops: Imagine you want to display the numbers 1 through 5. You could write this:
console.log(1); console.log(2); console.log(3); console.log(4); console.log(5);
Now display all the numbers from 1 to 1000. Loops make this easy. The `for` loop has a very specific syntax. It begins with a statement to initialize a counter variable. The next statement is a boolean which determines whether it should continue looping. The final statement changes the counter, hopefully in a direction that leads to it being closer to terminating. Example:
// exactly the same outcome as the code above for (var counter = 1; counter <= 5; counter++) { console.log(counter); } // this says... // - starting the counter at 1 // - while the counter is less than or equal to 5 // - execute the code inside the loop // - then increment the counter (counter++)
Aside: In loops you will typically see the increment operator after the variable. We could have used ++counter instead of counter++ and it would work the same. Here you can see the difference between prepending and appending the ++.
var three = 3; var eight = 8; console.log(three++); console.log(++eight); console.log('------------'); console.log(three); console.log(eight);
Both versions ended up incrementing their underlying variables in the end. But when ++ was at the end of the variable name, the change did not occur until after the initial variable was printed. Instead of counter++, we could also have written counter = counter + 1. The ++ operator is just more convenient. Back to loops: Loops are commonly used in association with arrays. Here we will display the contents of an array. Study this closely.
var plants = ['cactus', 'palm', 'shrub', 'grass']; for (var index = 0; index < plants.length; index++) { console.log(plants[index]); } // index starts at 0 and goes up until it stops being less than plants.length, which is 4 // so this loop does the following: // console.log(plants[0]); // console.log(plants[1]); // console.log(plants[2]); // console.log(plants[3]);
// compare the above code to this: for (var index = 0; index < plants.length; index++) { console.log(index); }
Recap:
var mySentence = "We are the knights who say"; var myArray = mySentence.split(' '); // split the sentence into an array of words myArray.push('ni!'); // add another element onto the array console.log(myArray); console.log(myArray[3].toUpperCase()); // grab element 3 (starting from 0), and print it in uppercase if (myArray.length > 6) { console.log('There are more than 6 words in the sentence'); } else { console.log('There were no more than 6 words in the sentence'); } console.log('The sentence contains words with the following lengths:'); for (var index = 0; index < myArray.length; index++) { console.log('ni'); }
Aside: Wondering what the "undefined" is that shows up whenever we use console.log? It is just the return value of that method and can be ignored. This editor always outputs the return value of the last computed statement, which is how it was possible to display certain values without using console.log earlier in the tutorial. Now you have all of the tools needed to create a function that gets the longest word of a sentence and displays it. There are many ways to do this, but if you do not know where to begin, here is one idea: - Split the sentence string into an array of words - Create a variable to hold your current maximum length word - Assume the first word (at index 0) is the maximum length word - Loop through each word in the array - if the current word is longer than the word in your maximum variable - set maximum to the current word - Display the value of the variable containing the longest word
var maximum; // you will set this later var sentence = "My name is Ozymandias, king of kings"; // YOUR CODE HERE
If you are still stuck, see this program that prints the smallest number in an array: https://tonicdev.com/sebastian/572c451cd1dc1011007d091b
Loading…

no comments

    sign in to comment