Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:53:58+00:00 2026-06-15T10:53:58+00:00

My Code: I tried the following code var str = Have fun storming the

  • 0

My Code:

I tried the following code

var str = "Have fun storming the castle!"
var character = "!";

function endsWith(str, character) {
    return str.indexOf(character, str.length - character.length) !== -1;
}

alert(endsWith(str, character));

It is giving the result as True since such word is present in the sentence.

But I need the result as castle! as it is the only word having such character atlast. If multiple words having such character also, I need those words.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:53:59+00:00Added an answer on June 15, 2026 at 10:53 am

    The function below takes the string and character, and returns an array of words that have the character as their last character. It is case sensitive but if you wanted to make it case insensitive then just add .toLowerCase() to the comparison line.

    function getWords(str, character)
    {
        var words = str.split(" ");
        var result = [];
    
        for(var i=0; i<words.length; i++)
        {
            if(words[i].slice(-1) == character)
            {
                result.push(words[i]);
            }
        }
    
        return result;
    }
    
    var str = "Have fun storming the castle!";
    var character = "!";
    
    console.log( getWords(str, character) );
    

    Outputs

    [“castle!”]

    Multiple words test:

    console.log( getWords("Have fun storming! the castle!", "!") );
    

    Outputs

    [“storming!”, “castle!”]

    jsFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried the following code: $('a.buildMenu').click(function (event) { // Prevent normal behaviour event.preventDefault();
My Code: I tried the following code var str=I like green and want to
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I am currently working on Problem 62 I have tried the following code to
I tried the following code: var origh1 = $('img:first').height(); alert(origh1); But it alerted a
I have the following code var distributionUnits = _distributionUnitRepository.FindByCompanyId(userSession.CompanyId); This returns 2 records. However
I want to validate password field. I have tried following code, but getting message
I have the following code: @{ var index=0; } @foreach (AdminSummary adminSummary in @Model.AdminSummaries)
I have variable var str as following: var str = <option value=1>tea</option>; I would
I have following client code to upload image from phone $(#placeorder).click( function() { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.