0

Im writing a program that will tell you if the input string contains a period, question mark, colon or space. If it doesn't it'll return "false". If it does, it'll return all of the characters before the found punctuation. Ex. str= "grey cat", program will return "grey" because it was stopped by the space. Can I use "or" in Javascript? EDIT Im trying to do this without any built-in functions. Im not looking for an efficient way

update - now it's just printing the str. How do I get it to just print what comes before the punctuation (if any)

function find_punctuation(str){
   let result = "";
   for (let i = 0; i < str.length; i ++ )
      if (str[i] === "." || str[i] === "?"|| str[i] === "" || str[i] === ","){
      }
      else result += str[i]

   return result
}
console.log(find_punctuation('he. y'));
  • a more compact way would be to use regex – Swetank Poddar Jun 10 '20 at 19:15
  • Does this answer your question? [How do I split a string with multiple separators in javascript?](https://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – WOUNDEDStevenJones Jun 10 '20 at 19:15
  • Is this an exercise? Or are you looking for something efficient? – rid Jun 10 '20 at 19:16
  • Based on the linked question, you can do `temp = str.split(/[\.\?;\s]+/)` and then either compare `temp[0].length` to `str.length` to determine if any punctuation was found, or return `temp[0]`. – WOUNDEDStevenJones Jun 10 '20 at 19:17
  • I shouldve been more specific. Its an exercise, I am avoiding using any built-in functions at this point. I wrote this program in scratch now im trying to do it in Javascript. I cant use .match, .split or any other built in functions – guywhogames Jun 10 '20 at 19:20

3 Answers3

1

function find_punctuation(str) {
  let result = "";

  for (let i = 0; i < str.length; i++) {
    // Determine if the current character is punctuation.
    const is_punctuation = (
      str[i] === "." ||
      str[i] === "?" ||
      str[i] === " " ||
      str[i] === ","
    );

    if (is_punctuation) {
      // If the current character is punctuation, then exit the loop.
      break;
    } else {
      // Otherwise, keep adding to the result string.
      result += str[i];
    }
  }

  // The result string will now contain all characters until the first
  // punctuation character, because the loop that added characters to
  // this string was exited as soon as the first punctuation character
  // was detected.
  return result;
}

console.log(find_punctuation('he. y'));
console.log(find_punctuation('hell,o'));
console.log(find_punctuation('hello'));
console.log(find_punctuation('cat?erpillar'));
console.log(find_punctuation('grey cat'));
rid
  • 61,078
  • 31
  • 152
  • 193
0

Use a regular expression:

function find_punctuation(str) {
  let result = str.match(/^(.*?)[.?: ]/);
  if (result) {
    return result[1];
  } else {
    return false;
  }
}
console.log(find_punctuation('he.y'));
console.log(find_punctuation('hey'));
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try this:

function find_punctuation(str){
   let result = "";
   for (let i = 0; i < str.length; i ++ )
      if (str[i] === "." || str[i] === "?" || str[i] === "  ") break
      else result += str[i]

   return result
}
console.log(find_punctuation('he.y'));
Jack
  • 955
  • 1
  • 9
  • 30