课程: JavaScript Practice: String Manipulation

Solution: Does the message include "fun"? - JavaScript教程

课程: JavaScript Practice: String Manipulation

Solution: Does the message include "fun"?

- [Instructor] In this challenge, we're checking feedback statements to see if any of them include the word "fun." To solve, we will need to use the string method "includes." this method takes an argument of a string that checks to see if it's included in the string in question. The method returns a Boolean indicating whether or not the search string was found. Additionally, we will return the string is positive on line 10 or negative on line 11 in all uppercase, depending on whether or not the statement includes "fun." This is a great opportunity to build on your knowledge from previous lessons. So the first thing we'll want to do is check to see if the message includes "fun." So we have our function on line 13, and then on line 15 we're going to go ahead and write return message.includes, and then we're going to pass includes the string "fun." So the next thing we want to do is return the variable is positive or negative, depending on whether or not the message includes "fun." So we'll use a ternary for this to make it nice and clean. So we'll add our question mark here. And then if it resolves to true, we're going to return positive, and then we'll add our colon. And if it's false, we'll return negative. So we're not done here, right? Because the other part of this is we want to make the variables positive and negative. We want to transform those to uppercase. So here we're going to apply our toUpperCase string method. And then with negative, we're going to do the same thing: toUpperCase. Alright, so a quick recap here. So if the message includes "fun," we're going to return the variable positive, which also happens to be the string positive and make that all uppercase. If not, we're going to return negative, which is the string negative, and that's going to be toUpperCase. So let's go ahead and run this. And marvelous work. It returned positive because the message includes the word "fun." Great job.

内容