2

I need to write JS function which returns true if string contains - depreciated in its last otherwise false.

For example:

var somestring = "string value - depreciated";

function should return true in above example.

function isDepreciated(var S)
{
    //Need to check for substring in last
    //return true or false
}

One possible solution is to use search function but that means that if - depreciated comes within string then it will also return true. I really need to find weather substring is in last or not.

Please help.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Azeem
  • 2,904
  • 18
  • 54
  • 89
  • Homework? What have you tried? – Anders Lindahl Jul 29 '13 at 08:37
  • 1
    Learn about `match` and `RegExp` and try them out. – mohkhan Jul 29 '13 at 08:39
  • You could also do this by using [`substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) to isolate the last X characters of the input and see if they match what you are looking for. – Jon Jul 29 '13 at 08:40

9 Answers9

2

Add the below code in your JS

function isDepreciated(string){
   return  /(-depreciated)$/.test(string);
}
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
Avneesh Raghav
  • 319
  • 2
  • 11
  • A regular expression is not necessary. – Ja͢ck Jul 29 '13 at 08:53
  • Also, should probably escape the `-`, though it's not necessarily required. – Evan Trimboli Jul 29 '13 at 08:57
  • Yeah! It might not be, but I find this is the shortest and robust line of code to solve this problem. – Avneesh Raghav Jul 29 '13 at 08:57
  • @Jack why not necessary? – meze Jul 29 '13 at 08:58
  • @meze I've marked this question as a duplicate of a more generic problem; you can find the solution there as well, or look at Marginean Vlad's answer. – Ja͢ck Jul 29 '13 at 08:59
  • @Jack that doesn't explain why not regexp. It takes less characters and probably will be faster. – meze Jul 29 '13 at 09:02
  • @meze I prefer finding simple functions that solve the exact problem first before moving to regular expressions as an alternative to string domain problems. This has nothing to do with speed; if it did, you'd probably be wrong though :) – Ja͢ck Jul 29 '13 at 09:10
1

You'll want to use the Javascript string method .substr() combined with the .length property.

function isDepreciated(var id)
{
    var id = "string value - depreciated";
    var lastdepreciated = id.substr(id.length - 13); // => "- depreciated"
    //return true or false check for true or flase
}

This gets the characters starting at id.length - 13 and, since the second argument for .substr() is omitted, continues to the end of the string.

Ghostman
  • 6,042
  • 9
  • 34
  • 53
  • Does negative indexing supported in IE versions below or equal to 8? – Azeem Jul 29 '13 at 08:53
  • @NidaSulheri yes u can ... kindly check this link http://stackoverflow.com/questions/6918943/substr-with-negative-value-not-working-in-ie – Ghostman Jul 29 '13 at 09:13
1
function isDepreciated(S) {
    var suffix = "- depreciated";
    return S.indexOf(suffix, S.length - suffix.length) !== -1;
}
Marginean Vlad
  • 329
  • 1
  • 6
1

You could use currying: http://ejohn.org/blog/partial-functions-in-javascript/

Function.prototype.curry = function() {
    var fn = this, args = Array.prototype.slice.call(arguments);
    return function() {
      return fn.apply(this, args.concat(
        Array.prototype.slice.call(arguments)));
    };
  };

With the helper curry function you could create your isDepricated check:

String.prototype.isDepricated = String.prototype.match.curry(/- depreciated$/);

"string value - depreciated".isDepricated();

Or use .bind():

var isDepricated = RegExp.prototype.test.bind(/- depreciated$/);

isDepricated("string value - depreciated");
jantimon
  • 36,840
  • 23
  • 122
  • 185
0
function isDepreciated(S){
    return (new RegExp(" - depriciated$").test(S));
}
Samurai
  • 3,724
  • 5
  • 27
  • 39
0

how about just use regular expression

  var myRe=/depreciated$/;
  var myval = "string value - depreciated";
  if (myRe.exec(myval)) {
    alert ('found');
  }
  else{
    alert('not found');
  }
David Zhan Liu
  • 474
  • 5
  • 8
0

lots of answers are already here (the one with $ is preferred), even though i also had to write one, so it will also do your job,

var somestring = "string value - depreciated";
var pattern="- depreciated";

function isDepreciated(var s)
{
    b=s.substring(s.length-pattern.length,s.length)==pattern;
}
schnill
  • 905
  • 1
  • 5
  • 12
-1

Ok, I haven't run this code on a browser, but this should give a basic idea of what to do. You might have to tweak some of the conditions if needed.

var search = "- depricated";
var pos = str.indexOf(search);

if(pos > 0 && pos + search.length == str.length){
    return true;
}
else{
   return false;
}

Edit: indexOf() returns the start index of the string.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
  • pos > 0, so that the string should be found and the next condition to see whether it is located at the end. A cleaner approach would be to do regex as posted by @Avneesh Raghav. – midhunhk Jul 29 '13 at 08:45
  • 1
    `contains` returns boolean and isn't supported in all browsers ;( – meze Jul 29 '13 at 08:49
  • Edited to indexOf(), thanks for spotting that. – midhunhk Jul 29 '13 at 08:54
  • Why the `if (foo) return true; else return false;` Why not just return the evaluation of the condition (`return foo;`)? A nitpick, but it seems rather redundant. – Evan Trimboli Jul 29 '13 at 08:58
  • Since the OP seems to be a student, I thought it would be easier to understand. – midhunhk Jul 29 '13 at 09:02
-1
    function isDeprecated(str) {
          return ((str.indexOf("- depreciated") == str.length - "- depreciated".length) ? true : false);
    }

    isDeprecated("this")
    false

    isDeprecated("this - depreciated")
    true

    isDeprecated("this - depreciated abc")
    false
v2b
  • 1,436
  • 9
  • 15