JavaScript code for calculating percentage equally distributed

const convertPercent = (num, param) => {
    const array = [];
    for(let i=(param ? 0 : 1); i <= num; i++) {
        array.push( (param ? i : 1) * 100 / num );
    }
    return array;
}

console.log( convertPercent(7,true)[5] );

Answer №1

Exploring a potential solution by breaking down "7 split into 7 parts each representing the percentage out of 100"

function calculatePercentages(number){
    for(var i=1,result=[]; i<=number; i++){
        result.push(i * 100 / number);
    }
    return result;
}

console.log( calculatePercentages(7) );

Here is the resulting array:

Array [ 14.285714285714286, 28.571428571428573, 42.857142857142854, 57.142857142857146, 71.42857142857143, 85.71428571428571, 100 ]

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

How to Use Fetch API to Send Files Using Form-Data

I've been facing challenges connecting my React frontend to my express and MongoDB backend. While using Postman was successful for testing requests, I encountered issues when trying to use fetch and Axios. Here is the postman request that worked: htt ...

Is it possible to utilize Sequelize model methods to articulate the given SQL query effectively?

I've been spending hours trying to convert the following SQL query into a Sequelize model method like findOne() - SELECT * FROM "Tariffs" WHERE "tariffType" = 'DateRange' AND (('${body.startDate}' BETWEEN "startDate" AND "endDate" ...

Avoid repeated appending of data in JavaScript and HTML

I am working with an HTML table and I need to ensure that the values in the second column do not repeat within the grid. Below is the JavaScript code I have written for this purpose: var $addedProductCodes = []; function getProductData(value){ $t ...

Object.assign versus the assignment operator (i.e. =) when working with React components

Just a quick question: I've come across some answers like this one discussing the variances between Object.assign and the assignment operator (i.e. =) and grasp all the points made such as object copying versus address assignment. I'm trying to ...

node.js experiencing crashing following loop iteration

I'm currently developing a performance testing tool using node.js to automate the process and store the results in MySQL. This tool is designed to track the load time of specific web pages in a browser, with the measurement displayed in seconds. I am ...

Steps to create folders in dat.gui:

I'm attempting to incorporate folders for the lights in a three.js project that I borrowed from a three.js example. However, I am struggling to make it work. From what I gather, I should use f1=add.folder('light1') and then somehow add the p ...

How can you use React.js to only display "Loading..." on the page while the full name is included in the URL?

I've hit a roadblock while trying to solve this issue. Here's the situation: On the page where I need to display certain information, I intended to showcase the full name of the individual from a previous form submission. However, instead of seei ...

Dealing with a 409 conflict situation involving a document in Node.js using Nano library

After conducting research, it has come to my attention that there are numerous document conflicts with couchdb. While exploring a potential solution in Updating a CouchDB document in nano, I discovered the following steps: Retrieve the document Store th ...

Preserving CSS styling while loading an HTML page with InnerHTML

By using the following code snippet, I have successfully implemented a feature on my website that allows me to load an HTML page into a specific div container when clicking a link in the menu. However, I encountered an issue where the background color of ...

Unable to dispatch actions within the mounted lifecycle hook in Vuex?

Can anyone explain why the json data I fetch with axios is not populating my state.pages as expected? Interestingly, when I make a change to the file and vite reloads, the data appears on the page. However, it disappears again upon refreshing the browser. ...

How to retrieve values from an array of objects using Javascript

I am working with an array of objects that contains value pairs. I am trying to access a specific value using the following syntax: myArray.code or myArray[0].code However, I am receiving an error message stating that myArray does not contain a property ...

What is the best way to incorporate an array of objects into the React state hook?

I have successfully retrieved data from the API in the specified format: { teamsregistered: [ { paid: false, _id: 602dea67451db71e71e4babd, name: 'MOH', mobile: '+919566879683', tag: 'JAR1NAH', __v: 0 }, { paid: false, _id: 6 ...

Bootstrap modal not displaying in full view

I recently ran into some issues while using a jQuery plugin with my bootstrap modal on my website. After implementing jQuery.noConflict(), I encountered a problem where the program no longer recognized $, forcing me to replace all instances of it with jQue ...

automatically dragging elements using jQuery UI

When implementing jquery UI draggable, I incorporate certain functionalities within the drag function. One specific task is scaling the dragging element based on its position. I am looking to automate the dragging of elements to specific coordinates (x, ...

Webpack compilation results in missing SVG icons display

As a newbie in web application development, I find myself in the final stages of my project. My current challenge involves bundling the entire project using webpack, and it's mostly smooth sailing except for one hiccup with my SVG icons. The issue se ...

Change a 24-hour time variable to 12-hour time using AngularJS/jQuery technology

I have a value retrieved from the database in a 24-hour time string format. I am seeking a solution to convert this string into a 12-hour clock time using either AngularJS or jQuery. Currently, I am unable to make any changes to the back-end (JAVA) or the ...

What is the best way to choose an Animatedmodal that can showcase various demos while using just one ID?

I am currently customizing my website and utilizing the AnimatedModal.js framework. I have successfully displayed content in one modal, but encountered difficulty when attempting to create multiple modals as they all share the same ID. My question is, how ...

Is it possible to further simplify this piece of JavaScript using jQuery?

In my journey to learn and grow with Javascript, I have come across a snippet of code that simply attaches an onclick event to a button and updates the text of an em tag below it. As I delve deeper into jQuery, I am beginning to appreciate its syntax and u ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

Can you provide instructions on how to configure the date format for the p:calendar component using client-side methods

Let's discuss the current scenario at hand. I have a JSF page where a date is displayed in the following manner: <h:outputText value="#{bean[date]}" > <f:convertDateTime pattern="#{Const.CALENDAR_PATTERN}"/> // featuring a ca ...