简体   繁体   中英

How to implement a function that will return when an intense string is passed in, and false otherwise?

Strings are intense if they end in three or more more ! marks. However, having ! marks anywhere but the end makes for a non-intense string.

The issue I'm having is when there is an ! in the middle of a string. The result should be false but it's still resulting as true.

My code:

function intenseString (str) {
  if (str.slice(-3) !== "!!!") {
    return false;
    } 
  else if(str.slice(str.indexOf("!"))){
    return false;
    }
  else if(str.slice(-3) === "!!!"){
    return true
  }
}

Use indexOf instead of slicing the string:

 const strings = ['abc!!!', 'abc!!de', 'abc!'] const intenseString = (str, chars) => str.indexOf(chars) >= 0 console.log(strings.map(x => intenseString(x, '!!!'))) 

Try one of these ways:

function intenseString(str) { return str.slice(-3) === '!!!'; }

function intenseString(str) { return str.endsWith('!!!'); }

function intenseString(str) { return /!{3}$/.test(str); }

Here's your code, with some formatting tweaks for readability:

function intenseString(str) {
  if (str.slice(-3) !== '!!!') {
    return false;
  }

  if (str.slice(str.indexOf('!'))) {
    return false;
  }

  if (str.slice(-3) === '!!!') {
    return true;
  }
}

Can you give some example inputs that return true ? I don't think this will ever return true because the second if statement says "return false if there are any ! 's in the string, which is implied by passing the first if .

I believe you meant to add + 1 : if (str.slice(str.indexOf('!') + 1)) return false which says "return false if there are any ! 's that are not the last character in the string", which still won't work: The first if statement will return false if the string doesn't end with !!! meaning that the smallest string that would get to the second if statement is !!! and there will always be characters after the first ! .

Another potential attempt would be to only check the string before the last three characters if (str.slice(0, -3).slice(str.indexOf('!') + 1)) return false , which almost works except for strings containing more than 4 ! at the very end... (such as !!!!! ).

I don't see a simple way (without regex to check that the remaining characters are only ! ) to make the second if statement work without looping over the string.

Note that your final if is unnecessary. Your first two are checking for failure and if they pass through them, it must be an intenseString. Also the string must end in !!! if it got past the first if .


Here's a potential solution which goes through every character in the string, except for the last 4, and returns false if there is ever an ! followed by something that is not an ! .

function intenseString(str) {
   if (!str.endsWith('!!!')) {
       return false;
   }

   for (let i = 0; i < str.length - 4; i++) {
       if (str.charAt(i) === '!' && str.charAt(i + 1) !== ('!')) {
           return false;
       }
   }

   return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM