What is the method for determining the level based on the provided experience points?

I've created a formula that can calculate experience based on specific levels and another formula that calculates the level based on given experience. However, there seems to be an issue with the second function as it is not returning the expected value.

const totalLevels = 40;
const xpForFirstLevel = 1000;
const xpForLastLevel = 1000000;

const getExperienceValue = level => {
  const BValue = Math.log(xpForLastLevel / xpForFirstLevel) / (totalLevels - 1);
  const AValue = xpForFirstLevel / (Math.exp(BValue) - 1.0);

  const oldXp = Math.round(AValue * Math.exp(BValue * (level - 1)));
  const newExp = Math.round(AValue * Math.exp(BValue * level));

  return newExp - oldXp;
};

const getLevelBasedOnExp = experience => {
  const BValue = Math.log(xpForLastLevel / xpForFirstLevel) / (totalLevels - 1);
  const AValue = xpForFirstLevel / (Math.exp(BValue) - 1.0);

  return Math.ceil(Math.log(experience / AValue) / BValue);
};

console.log(getLevelBasedOnExp(xpForFirstLevel)); // -9
console.log(getLevelBasedOnExp(xpForLastLevel)); // 30

The expected results are supposed to be 1 and 40, however, the actual output is -9 and 30.

Is there anyone who can provide assistance?

Can someone offer insight into this matter?

Answer №1

After experimenting with different equations, I have come to the conclusion that a slight adjustment in the formula yields more accurate results.

const calculateLevel = xp => {
  const B = Math.log(last_level_xp / first_level_xp) / (total_levels + 1);
  const A = first_level_xp - 1;
  return Math.ceil(Math.log(xp / A) / B);
};

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

Step-by-step guide on adding data to an arraylist using JavaScript

My ajax callback function receives a json object (arraylist parsed into json in another servlet) as a response, and then iterates through it. Ajax section: $.ajax({ url:'ServiceToFetchDocType', data: {" ...

The use of async components in Vue.js with named imports

I understand how to dynamically load components asynchronously in Vue. For example, instead of importing MyComponent directly: import MyComponent from '@/components/MyComponent' export default { components: { MyComponent } } We can use ...

JavaScript method that accepts two functions as arguments

What is the proper syntax for passing two or more functions to a method, like in this example setInterval("javascript function",milliseconds); is the following correct? setInterval("pushmarkers();clearOverlays();loadmarkers();",5000); ...

Can the parent document interact with Shadow DOM elements?

When it comes to user-created shadow DOM elements, this question focuses more on accessibility using the date input type: For instance, let's say there is a date input on a webpage. After some editing, the shadow DOM markup for this element (in Chrom ...

Experiencing a TypeError message while attempting to send header data with the nodejs request module

function loginUser(req, res) { console.log(req.body.loginemail); console.log(req.body.loginpassword); var options = { uri: webServiceURL.login,   method: "POST", body: { "login": req.body.loginemail, ...

What benefits does Observable provide compared to a standard Array?

In my experience with Angular, I have utilized Observables in the state layer to manage and distribute app data across different components. I believed that by using observables, the data would automatically update in the template whenever it changed, elim ...

Encountering a 404 error while using THREE.ImageUtils.loadTexture in three.js

My first attempt at using three.js involves loading a 3D model, but I'm encountering an issue with texture loading. loader.load('models/asteroid_OBJ/asteroid OBJ.js', function (geometry, materials) { var material = new THREE.MeshLambertM ...

Utilizing Sharepoint - accessing the Sp.Web.currentUser Property

I'm diving into a website I've been tasked to explore. While my HTML skills are limited, I'm eager to retrieve some SharePoint information using JavaScript. I launched my console and experimented with: var value = SP.Web.get_currentUser();, ...

Vue.js Class-based Decorator not Transmitting Event from Child to Parent Component

I'm fairly new to working with Vue.js and I've encountered an issue with the child to parent emit functionality. Essentially, I have a Box component that contains a Search component. In the Search component, I attempted the following: @Watch("se ...

Retrieving data from Firebase query and displaying as an array of results

Currently utilizing the @react-native-firebase wrapper for interaction with Firebase's Firestore. Within my function, some querying to a specific collection is performed, with the expected result being an Array object containing each located document. ...

Unraveling the complexities of double-encoded UTF-8 in PHP

Trying to transmit data from an HTML page via Ajax to a PHP page. This is the jQuery code I'm using: $.ajax({ url: "test.php", type: "POST", data: { name: "João" } }).done(function (data) { alert(data); }) When sending ...

Using jest in typescript to simulate HttpRequest body and InvocationContext in @azure/functions

I have the following function and I am trying to write a test for it, but I'm having trouble figuring out how to mock HttpRequest import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions"; export async function ...

Test fails in Jest - component creation test result is undefined

I am currently working on writing a Jest test to verify the creation of a component in Angular. However, when I execute the test, it returns undefined with the following message: OrderDetailsDeliveryTabComponent › should create expect(received).toBeTru ...

Guide on establishing two loops in React JS

I'm receiving a JSON array that contains nested arrays. I attempted to iterate through it using two loops, but so far, I haven't been successful. {this.state.listOfAlarms && this.state.listOfAlarms.map((alarms) => {alarms.repo ...

Organize and categorize items

I need help sorting an object displayed below. My goal is to calculate the sum of all rating properties for each object, and then sort the objects based on the highest total rating. For instance, if the total rating for Intro 1 is 7 and for Intro 2 is 3, ...

External Submit button malfunctioning when attempting to submit two distinct forms

I'm currently working on a program to add items to the cart, and I need to include product attributes like colors and sizes in the process. However, I seem to be encountering an issue where only one form is submitted when using jQuery's submit(), ...

What steps should I take to show a particular set of data upon selecting a checkbox component?

I have a table with a column named status, which can be in progress, pending, or dispensed. My goal is to filter the data based on the checkbox that is selected above the table. For instance, if I check the "pending" checkbox, only the data with the pendi ...

Is it possible to utilize $.each() in combination with $.ajax() to query an API?

I am dealing with an array containing 2 values, and for each value, I need to make an AJAX query to an API to check the stock availability. If there is stock for both values, a certain message should be executed, otherwise, a different message. This check ...

What is the process for configuring my form to automatically send to my email upon clicking the send button?

I found this code snippet on a website and I'm trying to figure out how to make the 'Send!' button redirect users to my email address with their message, name, and email included. Can anyone help me solve this issue? I attempted to add my e ...

AngularJS - Increase the input date by 6 hours before converting it to JSON

Is there a way to add 6 hours to an input date in AngularJS? I attempted the following code: var eventStart = new Date ($scope.event.startdateid); var startDate = new Date ( eventStart ); startDate.setHours ( eventStart.getHours() + 6 ); event.startdate ...