The fixed method in JavaScript is a handy tool for converting

Is it possible to implement the toFixed() method only when the input strings exceed 12 characters in length? If not, I would like the input to display normally, resembling a standard calculator app. I have experimented with a maximum character method, but the results are unsatisfactory and should not be relied upon.

https://i.sstatic.net/8yw1t.png

$(document).ready(function(){

var inputs=[""];

var totalString;

var operators1=["+", "-", "/", "*"];
//operators array including "." for validation
var operators2=["."];

var nums = [0,1,2,3,4,5,6,7,8,9];

function getValue(input){
if(operators2.includes(inputs[inputs.length-1])===true && input==="."){
  console.log("Duplicate '.'");
}
else if(inputs.length===1 && operators1.includes(input)===false)
        {
          inputs.push(input);
        }
else if(operators1.includes(inputs[inputs.length-1])===false){
  inputs.push(input);
}
else if(nums.includes(Number(input))){
  inputs.push(input);
}
update();
}
function update(){
totalString = inputs.join("");
$("#steps").html(totalString);
console.log(inputs);
}
function getTotal(){
totalString = inputs.join("");
$("#steps").html(eval(totalString));

}
$("a").on("click", function(){
if(this.id==="deleteAll"){
  inputs=[""];
  update();
}
else if(this.id==="backOne"){
  inputs.pop();
  update();

}
else if(this.id==="total"){
  getTotal();

}

else{

  if(inputs[inputs.length-1].indexOf("+","-","/","*")===-1)
  {
    getValue(this.id);
  }
    else
    {
      getValue(this.id);
    }
}
});

});

Answer №1

In the event that you must utilize the toFixed function, you could implement it as follows:

let num = "333453453453453453";
num.length > 12 && (+num).toFixed();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conditional statement in PHP based on the outcome of a SQL query

In order to use PHP Mailer to send an email, I need to first validate data in 3 different tables. Here is the code snippet I have: //Querying the 2 tables to check today's data and checking the third table to see if the email has already been sent $ ...

Array of Geographical Location Data Provided by Google Maps Geocoding

Utilizing a library for Google Geocoding API Wrappers (https://code.google.com/p/gmaps-api-net/) to retrieve or map a full address may sometimes result in inaccuracies. This is often due to missing address types returned by Google, causing discrepancies in ...

Having issues with Bootstrap flip div not functioning properly when clicked on a mobile device?

I am currently working on a website and encountering an issue. Here is the code snippet I am using: https://jsfiddle.net/3caq0L8u/ The objective is to flip a div when a button is clicked. The button responsible for flipping the div resides in both the " ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

transform the outcome of a $lookup operation into an object rather than an array

When performing a $lookup from a _id, the result is always 1 document. This means that I would like the result to be an object instead of an array with one item. let query = mongoose.model('Discipline').aggregate([ { $match: { ...

Utilize jQuery to dynamically load and assign unique ids to elements within an array

I am seeking assistance with dynamically assigning unique IDs to elements in an array using JavaScript and jQuery. I am new to these languages and need some guidance. function assignIds() { var elementIds = ['name', 'lname', ' ...

What is the best way to access data from this $scope in AngularJS?

Upon printing selecteditems to the console, this is the output: [{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}] I have stored it in $scope.details as follows: var selecteditems = $location.search().items ...

Using AJAX to Send Requests to PHP

Embarking on my first ajax project, I believe I am close to resolving an issue but require some guidance. The webpage file below features an input field where users can enter their email address. Upon submission, the ajax doWork() function should trigger t ...

What is the best way to update and override multiple nested NPM dependencies with various versions?

Apologies for not being fluent in English Here is my NPM dependency: dependency tree +-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c1e090d0f184108091a4105021f1c090f18031e2c5d4255425c">[email protected]</a> ...

What is the proper way to include jQuery script in HTML document?

I am facing an issue with the banners on my website. When viewed on mobile devices, the SWF banner does not show up. In this situation, I want to display an <img> tag instead, but the jQuery code is not functioning correctly. My template structure l ...

Having difficulty obtaining the necessary indexes from several NumPy arrays

I am trying to find the index of the last array (d) where the elements are smaller than 20, but those indices should be within a region where elements of array 'a' are 1 and both arrays 'b' and 'c' have values other than 1. H ...

Error: Supabase and Next.js encountered a self-signed certificate within the certificate chain causing an AuthRetryableFetchError

Encountering an error related to certificates while attempting to retrieve the user from Supabase within getServerSideProps using Next.js: AuthRetryableFetchError: request to https://[redacted].supabase.co/auth/v1/user failed, reason: self signed certifica ...

maintaining a specific variable within React

I am working on integrating pagination into my app. I have written the code below, but unfortunately, I am encountering an issue. Below is the implementation of my useEffect: useEffect(() => { let x = null; const unsubscribe = chatsRef . ...

Ways to eliminate dates from the text of listed items

Before finalizing their registration, users on our site are shown a review page. This panel displays all the items they have selected, creating a unique and variable list for each individual. However, each item in the list starts with a distracting date/ti ...

What mechanism does package.json use to determine whether you are currently operating in development or production mode?

What is the process for package_json to determine when to load devDependencies as opposed to regular dependencies? How does it differentiate between local development and production environments? ...

Using ajax for sending data is a breeze, but I am encountering trouble when attempting to receive data back from

I have a function that retrieves a value from an input and sends data through ajax to another PHP file. However, I am facing an issue where I cannot retrieve the result back from the PHP file even though I echo it in the ajax success function. <script&g ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

How come attempting to read a nonexistent document from Firestore results in an uncaught promise?

I've been struggling to read and display data from Firestore, but I keep seeing error messages in the console. Encountered (in promise) a TypeError: Unable to read properties of undefined (reading 'ex') Encountered (in promise) a TypeError ...

Implementing a Countdown Clock in Auction Listings

Similar Question: Countdown to a specific date Is there a way to implement a jQuery countdown timer that starts from the day of posting an advertisement and ends on the expiry date? ...

What is the best way to implement rate limiting or throttling on a Strapi API?

Our company relies on a simple strapi API implemented in node.js and hosted on Heroku. Despite our efforts, we have not been able to find a solution to implement rate limiting, as it appears that Heroku does not offer throttling add-ons and strapi lacks bu ...