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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:21:35+00:00 2026-06-03T14:21:35+00:00

I am trying to do something I though would be really simple. add the

  • 0

I am trying to do something I though would be really simple. add the value of 1 to a number.

However the number was plain text and things are getting a little confusing. As you may be able to guess, I am fairly new to this.

My goal is to add 1 point to an existing score, the score has one decimal place, eg 1.3.
So the desired result after adding a point is 2.3. However The current script I wrote is returning adds a point to the second decimal place and I don’t understand why.

Thanks for the help.

var getPoints = parseInt($('.awesome-points').text(), 10).toFixed(1);
alert(getPoints);
var addPoint = 1;
var newScore = getPoints + addPoint;
$('.awesome-points').text(newScore);

Mark

  • 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-03T14:21:40+00:00Added an answer on June 3, 2026 at 2:21 pm

    .toFixed() returns a string (see the MDN documentation), that’s why getPoints + addPoint performs string concatenation, not addition.

    Call that method after you performed the addition:

    // unary + converts any numerical string to a number (no matter whether float
    // or int)
    var getPoints = +$('.awesome-points').text();
    var newScore = getPoints + 1;
    $('.awesome-points').text(newScore.toFixed(1));
    

    Or more concise :

    $('.awesome-points').text(function(i, val) {
        return (+val + 1).toFixed(1);
    });
    

    How this works:

    Instead of passing a value to .text(), you can pass a function. This function will be executed for each of the selected elements. The first argument passed to the function is the index of the element in the set of selected elements and the second argument is the current text content. The return value will be set as new text content.
    This is a good way to read and write the text content, without invoking .text() twice.

    (+val + 1).toFixed(1) is evaluated as follows: First val is converted to a number through the unary plus (+val) and then increased by one (+ 1). .toFixed(1) is called on the result of that operation.

    +val will return NaN if the text cannot successfully be converted into a number. I assume this won’t happen in your case, but this can easily be dealt with with the expression

    return ((+val || 0) + 1).toFixed(1); 
    

    That means, in case +val evaluates to false (which includes NaN), we use 0 instead. It’s a common pattern in JavaScript, see also What does "options = options || {}" mean in Javascript?.

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

Sidebar

Related Questions

I'm trying to do something which I figure would be rather easy, though I'm
I'm trying to do something which I thought would be fairly simple. Get IIS
actually I thought I was trying something really simple. ControllerClassNameHandlerMapping sounded great to produce
I'm trying to do something I thought would be relatively simple: Upload an image
I'm trying to do something that I thought would be quite simple but as
I am trying to do something I thought would be simple. I have a
I am trying something really simple in F# to try and test interaction with
In Flash ActionScript 3, I am trying to do something I thought was simple:
I'm trying to do something that I thought ought to be reasonably simple but
I've been trying to make a little simple game just to test my logics,

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.