Longest Word

node v0.12.18
version: 3.0.0
endpointsharetweet
After this you will be able to write a program that takes a sentence and displays the longest word in that sentence. You will become familiar with: - Strings - Arrays - Numbers - Booleans - Conditionals - Loops Strings are sequences of characters wrapped in quotes. For example, we can create a variable called helloString that contains the words "Hello world!":
var helloString = 'Hello world!';
The quotes can be single or double quotes, but we'll use single quotes here. (In javascript the semicolon is optional, too.) Strings come with some built-in properties and methods. For example:
// .length property (number of characters in the string) helloString.length;
// .toUpperCase() method helloString.toUpperCase();
// .toLowerCase() method helloString.toLowerCase();
// .replace(substringRemoved, stringInserted) method // where `substringRemoved` is the part of the string you want to replace // with `stringInserted` helloString.replace('Hello', 'Goodbye');
*Properties* (like the length property) can be thought of as dependent variables within your variable. Whenever you change your string, the length property will update. You don't need parentheses to access properties. *Methods* can either directly change your variable, or (as is the case with the methods we've used so far) return a new value from some transformation on your variable. You'll notice that after calling toUpperCase() on our string, the original string remains unchanged. Not all methods behave this way, as you will see soon. A brief note: it is also possible to have an empty string. The empty string is denoted by two quotes with nothing in between them.
var emptyString = '';
// Define your own string here, then try out any of the methods on it // Press "Run" when you are done to see the result of your final line of code // YOUR CODE HERE // eg. var myString = "" // myString.length // myString.toUpperCase() // myString.replace(string1, string2)
Strings are one of many types in JavaScript. Another type is the Array, which is a sequential list. Arrays are written with brackets [ ] and can contain items of any type. Here is an array of numbers:
var numbers = [10, 20, 5.5, -12, 22];
Here is an array of strings
var strings = ['Washington', 'Adams', 'Jefferson', helloString];
Just like strings, Arrays come with properties and methods. (Tip: Peek into the "Array Prototype" in the code section above to see a list of all of them.)
numbers.length;
strings.length;
Side note: so far, the last line of code in any block is what gets displayed. In order to output a variable from any line, use the console.log() method. See below:
console.log("hello"); console.log(1); var x = 10; console.log(x); var y = [1, 3, 5]; console.log(y); "(Since this string is the last line it will also be displayed)"
Now back to arrays. A specific item in an array can be accessed as long as you know the index of its location (starting from zero). Use brackets containing the index of the item in the array in order to retrieve it.
// Recall that numbers was defined above as: var numbers = [10, 20, 5.5, -12, 22]; var x = numbers[0]; console.log('x is: ', x); var y = numbers.length; console.log('there are this many numbers: ', y); var lastNumber = numbers[numbers.length - 1]; console.log('the last number in the list is: ', lastNumber);
// Create an array and access one of its items: // YOUR CODE HERE
Items can be added to the end of an array with the push() method
// Recall that our strings array was defined as: var strings = ['Washington', 'Adams', 'Jefferson', helloString]; strings.push('Madison'); var president = 'Monroe'; strings.push(president); console.log(strings); // console.log allows you to display any variable
Create an Array of strings here, then display the last item in the array using console.log()
// Hint: the index of the last item is the array's length - 1 // YOUR CODE HERE
It is also possible to change a string into an array. Calling the split() method on any string splits that string into an array. This method takes a single parameter, which is the character you want to use to split the string by. Here we will split this string by the '/' character.
var weekdays = 'Monday/Tuesday/Wednesday/Thursday/Friday'; weekdays.split('/'); // this becomes ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Try splitting this string into an array. Think of which character is used as the delimiter here. The example above used "/". What character separates the words below?
var swedishNumbers = "ett två tre fyra"; // YOUR CODE HERE
Try splitting this string into an array. Note that the delimiter doesn't have to be one single character. Any string can be used to define the split points.
var japaneseNumbers = "ichi--ni--san"; // YOUR CODE HERE
Try getting an array of every character in this string using the split method:
var newString = 'Hint: use the empty string'; // YOUR CODE HERE
Once you have split a string into an array, you probably want to operate on it. This example shows that you can save the output of the split() method into a new variable, then use it
var weekdayArray = weekdays.split('/'); weekdayArray.length; // this shows that we have 5 weekdays in our array
Try splitting the weekdays array instead by the word 'day' and then save it to a variable.
// YOUR CODE HERE
Continue here: https://runkit.com/sebastian/longest-word-2
Loading…

no comments

    sign in to comment