-3

I'm new to programming and this question was on a entrance exam for a coding bootcamp. I'm curious about the answer:

Write a method not_string which takes a string and returns that string with the word "not " prepended to it UNLESS the original string already begins with the full word "not ".

For example:

not_string("Hi, this is a string")
#=>  "not Hi, this is a string"

not_string("not a string here")
#=> "not a string here"

not_string("nothing strange about this one")
#=> "not nothing strange about this one"
halfer
  • 19,824
  • 17
  • 99
  • 186
Ron
  • 1
  • 1

1 Answers1

0

That should be it:

var not_string = function(input) {
  if(typeof input !== 'string') {
    return 'please enter a string';
  }
  if(input.indexOf('not') === 0) {
    return input;
  }
  return 'not ' + input;
}
cyr_x
  • 13,987
  • 2
  • 32
  • 46