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.

3
  • Homework? What have you tried? Commented Jul 29, 2013 at 8:37
  • 1
    Learn about match and RegExp and try them out. Commented Jul 29, 2013 at 8:39
  • You could also do this by using substring to isolate the last X characters of the input and see if they match what you are looking for. Commented Jul 29, 2013 at 8:40

9 Answers 9

2

Add the below code in your JS

function isDepreciated(string){
   return  /(-depreciated)$/.test(string);
}
Sign up to request clarification or add additional context in comments.

7 Comments

A regular expression is not necessary.
Also, should probably escape the -, though it's not necessarily required.
Yeah! It might not be, but I find this is the shortest and robust line of code to solve this problem.
@Jack why not necessary?
@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.
|
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.

2 Comments

Does negative indexing supported in IE versions below or equal to 8?
@NidaSulheri yes u can ... kindly check this link stackoverflow.com/questions/6918943/…
1
function isDepreciated(S) {
    var suffix = "- depreciated";
    return S.indexOf(suffix, S.length - suffix.length) !== -1;
}

Comments

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");

3 Comments

The problem being solved is more specific, so I wouldn't modify the String prototype for that.
You are probably right so I added a bind example ;)
I see you're an avid fan of the JavaScript dynamism ;-) +1 for the creative aspect hehe
0
function isDepreciated(S){
    return (new RegExp(" - depriciated$").test(S));
}

Comments

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');
  }

Comments

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;
}

Comments

-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

Comments

-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.

5 Comments

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.
contains returns boolean and isn't supported in all browsers ;(
Edited to indexOf(), thanks for spotting that.
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.
Since the OP seems to be a student, I thought it would be easier to understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.