Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have array like this for example:
const arr = [3,5,7,10];
And I have a number, for example:
const status = 4;
I have to find index of this number, so number 4 is bigger than 3, and less then 5, so it belongs to second index, I have to get something like position.
Position for the number for should be 2. (it's bigger than 3, but less then 5).
Expected result
const position = 2;
are you looking for this simple solution?
const arr = [3,5,7,10];
const status = 4;
const position = arr.findIndex( e => e > status) + 1;
console.log(position)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to get only the value of "vp" from the query string only if it is between 0-4. If it is bigger (for example: 5, 12 etc.) I want to get undefined.
"?vp=324" // ==> undefined
"?vp=1&mn=345" // ==> 1
"?mn=345&vp=2" // ==> 2
"?sf=23&vp=12&fd=343" // ==> undefined
May or may not need to parseInt for this.
function smallVP(queryString) {
let params = new URLSearchParams(queryString);
let vp = parseInt(params.get("vp"), 10);
if (vp >= 0 && vp <= 4) {
return vp;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It may be silly but i need to write an logic to get integer to split to array based on size.
For example if integer is 13 i need to convert to array as [5,5,3] if my size is 5.
if size is 10 then array should be [10,3].
Can any one help me writing the logic in javascript?.
You may build up an array of size N/n if there's no remainder from division (N%n), or N/n+1 if there is one.
Then you may simply Array.prototype.map() through that array and populate the result with n or use remainder as the last element if one exists:
const num = 13,
size = 5,
splitNumber = (N, n) =>
[...Array(0|N/n+(N%n ? 1 : 0))]
.map((_,i,s) =>
N%n && i == s.length-1 ? N%n : n)
console.log(splitNumber(13,5))
console.log(splitNumber(13,10))
.as-console-wrapper{min-height:100%;}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of agents which each have the attribute rate which is their accepted calls minus their declined calls. I order these agents in the array based on the rate. I'd like to give agents with a better rate a higher chance to be selected for a call than other agents with a lower rate to make things fair. I understand how to select a random element from an array but I can't wrap my head around this problem.
In this code, there is a 2/15 chance to get 2, 3/15 chance to get 3, and so on.
15 is based on the total rate of all the agents.
var aoa = [{rate: 2},{rate: 3},{rate: 6},{rate: 4}];
var r = Math.random() * aoa.map(e => e.rate).reduce((ac,c) => ac+c,0);
var sa;
aoa.forEach((a,i) => {
var pt = aoa.slice(0,i).map(e => e.rate).reduce((ac,c) => ac+c,0);
if (r >= pt && r < pt+a.rate) sa = a;
console.log(`${r} has to be between ${pt} and ${pt+a.rate} if chosen rate = ${a.rate}`);
});
console.log(sa);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Var randomLetters = "ljhgfdza";
Var randomString = "";
Now I'll have to add elements in the first variable to the second Randomly
Using the while loop, and Math.floor(Math.random() * random letters.length)
I am having a problem at my
"While (condition)" what should it be?
To flat out answer your question, you can use:
while(randomLetters.length > 0){
Then when you use a letter from randomLetters, you delete the letter and the length is now 1 less.
This will be enough for you:
const randomLetters = "ljhgfdza";
const returnRandom = (randomString) => {
const arrString = [...randomString].sort((a, b) =>{
return 0.5 - Math.random()
}).join("");
console.log(typeof arrString,arrString);
}
returnRandom(randomLetters);
but... in this case sort method is not that random as you think. This link will tell you why. I would do this with reduce() or map(), both are described in link above.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 40 fruits and I want to distribute it randomly and store in array of length 10.
See below code inputs.
var fruits = 40;
var arr = [];
And I want output like this
arr = [2,5,1,0,0,3,6,10,0,13];
10 positions of array should be filled randomly but addition of values should be 40.
A recursive divide-and-conquer approach:
function distribute(length, value) {
if (length <= 1)
return [value];
var half = Math.floor(length / 2),
dist = Math.floor(Math.random() * value);
return distribute(half, dist).concat(distribute(length-half, value-dist));
}