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 720065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:42:57+00:00 2026-05-14T05:42:57+00:00

im trying to make a small name summary function depending on the size of

  • 0

im trying to make a small name summary function depending on the size of the elements container, here’s what I have;

function shorten_text(str, size){
    size = size.match( /[0-9]*/ );
    var endValue = Math.floor( Number(size) / 10 );
    var number;
    var newStr;
    for ( number = 0; number <= endValue; number++ ) {
        if( str[number].length != 0 ) {
            newStr += str[number];
        }
    }   
    return newStr + '...';

}
shorten_text('Phil Jackson', '94px');
// output should be 'Phil Jack...'

What I seem to get is undefinedundef…
can anyone see where I am going wrong?

EDIT:

revised code based on comments below for anyone who is googling for such function:

function shorten_text(str, size){
    size = parseInt(size);
    var endValue = Math.floor(size / 10);
    if( str.length > endValue ) {
        return str.substring(0, endValue) + '...';
    }else{
        return str;
    }
}

SCREEN SHOT:

screenshot

  • 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-05-14T05:42:58+00:00Added an answer on May 14, 2026 at 5:42 am

    You need to initialize your newStr variable with an empty string, otherwise that variable will contain the undefined value, which will be converted to string when you concatenate, e.g.:

    var test; // at this moment the variable contains the undefined value
    test += 'foo';
    // now test contains "undefinedfoo"
    

    In your function:

    function shorten_text(str, size){
        size = size.match( /[0-9]*/ );
        var endValue = Math.floor( Number(size) / 10 );
        var number;
        var newStr = '';
        for ( number = 0; number <= endValue; number++ ) {
            if( str[number].length != 0 ) {
                newStr += str[number];
            }
        }   
        return newStr + '...';
    }
    
    shorten_text('Phil Jackson', '94px'); //  outputs "Phil Jacks..."
    

    A few comments:

    • You don’t need to call Number(size), since the division operator makes type coercion. implicitly
    • You could use the substring method to get a portion of your original string.
    • Accessing characters of a string with the square bracket property accessor may not be supported by some implementations, you can use the standard charAt method (str.charAt(i))

    Another approach to do the same:

    function shorten_text(str, size){
        var endValue = Math.floor(parseInt(size) / 10);
        return str.substring(0, endValue) + '...';
    }
    
    shorten_text('Phil Jackson', '94px'); //  outputs "Phil Jack..." as expected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i.ToString("00") or i.ToString("000") depending on what you want Look at… May 15, 2026 at 2:33 am
  • Editorial Team
    Editorial Team added an answer Don't store passwords in cookies. You need to call session_start… May 15, 2026 at 2:33 am
  • Editorial Team
    Editorial Team added an answer If you are adding this RDFa markup to the event's… May 15, 2026 at 2:33 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.