Obtain the following ten years following a specified value

Seeking the optimal method to determine the next decade after a given value in Javascript. For instance:

If the value is 9, the next decade would be 10^1
For 200 it would be 10^3
And for 1001, it would be 10^4

This algorithm should also cater for negative numbers:

With -9, the next decade would be -10^1
For 200, it would be -10^3
And for 1001, it would be -10^4

The term "decade" may have been inaccurately used here. Basically, what I need is the next whole number that can be expressed as a power of 10. For example, for 9, that's 10^1. And for 200, that's 10^3 since 10^2 is less than 200...

Answer №1

To calculate the power of 10 for a given integer value, you can use the logarithm of 10 and add one to the result. It's important to consider the sign of the integer beforehand.

console.log([9, 200, 1001, -9].map(v => (v >= 0 || -1) * Math.pow(10, 1 + Math.floor(Math.log10(Math.abs(v))))));

Answer №2

To determine the power of 10 you need, simply take the ceiling of the logarithm in base 10:

function findPowerOfTen(number) {
  return (number > 0) ? Math.pow(10, Math.ceil(Math.log10(number))) :
                        -1 * Math.pow(10, Math.ceil(Math.log10(number * -1)));
}

console.log(findPowerOfTen(9));
console.log(findPowerOfTen(200));
console.log(findPowerOfTen(1001));
console.log(findPowerOfTen(-9));
console.log(findPowerOfTen(-200));
console.log(findPowerOfTen(-1001));

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

Marionette - Apply a class to the parent ItemView's tagname

I've been working with Bootstrap accordion panels and I'm trying to assign a class to the parent panel of the panel-collapse. Essentially, what I want to achieve is: if (child element) hasClass('panel-collapse.in') { this.addClass ...

What is the process of modifying the data in DataTables once it has already been initialized?

I am looking to dynamically populate a table using datatables through an ajax request. Here is the approach I have taken: $(function(e) { $('#CampaignMenu').change(function(e) { $('#ReportWrapper').hide(); ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Implementing a pull-to-refresh feature in React Native using Redux

I need to implement pull to refresh functionality, but I'm unsure of what to call from _Refresh(). My actions, constants, and reducers are stored on another page. How can I trigger the API again? Thank you in advance for your assistance. class Homewo ...

I'm having trouble retrieving data from the server using the AngularJS $http.get() function. What am I doing wrong

Ensure that your question is clear and that your code properly showcases the issue at hand. Feel free to leave any questions or comments below for clarification. app.js var express = require('express'); var app = express(); app.use(express.sta ...

Issue with ReactiveVar causing React component not to re-render

I am a beginner with React in Meteor. To summarize: When I change the value of my ReactiveVar in my data container, the view does not update. Here is the code snippet: import React, { Component, PropTypes } from 'react'; import ReactDOM from & ...

Jest-Native encountered an error: "SyntaxError: Unable to use import statement outside of a module"

Trying to perform unit testing using https://github.com/callstack/react-native-testing-library along with https://github.com/testing-library/jest-native. I am able to test plain JavaScript files without any issues, but I am facing an error when testing com ...

Utilizing AngularJS to Retrieve Row Information Upon Button Selection

I'm currently developing an application that includes a feature allowing users to run a query from a drop-down menu and view the data in a table. Each row in the table will have a button, and when clicked, I need to capture the values of that specific ...

What could be the reason for incorrect data being sent when using multi-select values with jQuery/A

I implemented a multi-select dropdown menu where users can select values. Whenever a user selects a value, a jQuery/AJAX request is sent to the server. Check out the code snippet below: $("#send").on("click", function() { var elem$ = $("#cars"), el ...

The PDF document appears quite different from the original HTML page it was created from

Is there a way to create a PDF document that mirrors the appearance of a web page using jsPdf? When attempting this, it seems that the font size, text alignment, and table alignment of the resulting PDF do not match that of the original web page. Additiona ...

Is it possible to dynamically load a specific div when the page loads, relying on the

I am using JQuery SlideUp and slideDown methods to toggle the visibility of panels. How can I load or display the first record's contact information as a default? Currently, it loads blank because I set the panel's display property to none: < ...

Downloading the file from the specified URL is taking too much time when using the save as blob function

I have developed a service to retrieve and save files from a specified URL. (function() { angular.module('SOME_APP') .service("downloadService", downloadService); function downloadService($http){ var downloadFil ...

Refresh information periodically within a Node.js application

Recently, I developed a web application using nodejs to fetch data from Amazon. My goal is to have the app update at regular intervals for different purposes: - Every 15 minutes for real-time inventory monitoring - Once daily for less frequently changing d ...

Live Search: Find Your Answers with Ajax

I've come across this issue multiple times, but haven't found a solution that fits my specific requirements. I've encountered several URLs with links like example.com/ajax/search?query=Thing I'm currently working on a header and using ...

The act of delegating ngshow and nghide functionality

check out this demo: http://plnkr.co/edit/EWOvKsTEutiveMEAGTKf?p=preview <li ng-show="showList" ng-repeat="task in tasks" ng-hide="task.checked=1"> {{task.name}} </li> Is it possible to use ng-show and ng-hide together on the same element? ...

What is the best way to convert an HTML table into an array of objects?

In my Angular Protractor end-to-end (e2e) tests, I need to perform assertions on an HTML fragment similar to the following: <table> <thead> <tr> <th>Name</th> <th>Age</th> ...

Adjust the package.json file for deployment

I've encountered a problem while attempting to deploy my nodejs application on Heroku. Despite following the documentation and modifying files in the root directory, I have not been successful. Below is the structure of my package.json file: { ...

What is the best way to handle the select event for a jQuery UI autocomplete when there are images involved?

Looking for help with creating an autocomplete feature with images on this jsfiddle. Despite trying to capture the event when a user selects an image, it doesn't seem to work properly: $("#input").autocomplete({ //source: tags, so ...

Compatibility Problems Arising Between NodeJS and MongoDB Drivers

Experimenting with MongoDB and NodeJS: I am attempting to import a JSON file from Reddit: Mongo Version: AMAC02PC0PHG3QP:dump macadmin$ mongo --version MongoDB shell version: 3.0.6 Mongo Driver Version: name: mongodb version: 1.3.23 Mongoose V ...

Guide to handling HTTP request parsing in Express/NodeJs when the content type is missing, by setting a default content type

Is it possible to retrieve POST data in an express request when the bodyParser does not work? var server = express(); server.use(express.bodyParser()); server.post('/api/v1', function(req, resp) { var body = req.body; //if request header doe ...