While loop and Strings [closed] - javascript

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.

Related

What is this handleChange method doing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
handleChange(e) {
const index = Number(e.target.id.substring(e.target.id.length - 1, 19));
const copyFormArray = JSON.parse(JSON.stringify(this.state.educationData));
copyFormArray[index][e.target.name] = e.target.value;
this.setState({
educationData: copyFormArray,
});
};
What does this handleChange method do, specifically in the index variable? I understand what substring and the Number constructor do but the second argument I cannot find any answers for this specific question although it works as intended.
Substring works in a tricky way, if second argument (in your case 19) is less than first argument - then they will be "swapped", and if not - it will just take text between indexes (argument1 and argument2).
So say for example i have a string:
var x = "aaaaa-5";
if i will write:
x.substring(x.length-1)
It will give me "5".
Then if i will write:
x.substring(3,6)
It will give me "aa-"
But then if i will write:
x.substring(6,3)
It will be exactly the same as previous call - "aa-"
So internally logic of substring is kind of like this:
function substring(start,end) {
if(start > end) {
var buf = end;
end = start;
start = buf;
}
//... other logic
}

JavaScript - Get array element position/index [closed]

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)

How to use regular expressions to say that you need to take the value of Categories = .... & Search = ..... & [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this line.
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
How to use regular expressions to say what you need to take from the beginning of a line and up to 2 characters &.Categories=All&Search=hat&
Use split and pass the limit argument, then just reconstruct it back into a string (if you really need the & - if not, discard everything after the split):
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
const res = x.split("&", 2).map(e => e + "&").join("");
console.log(res);
You can use /([^&]*&){2}/g regex to match what you want.
let x = "Categories=All&Search=hat&ListPage=15&Page=1";
let y = x.match(/([^&]*&){2}/g);
console.log(y);

Returns the cube of the sum of an input and 2 [closed]

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'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.

distribute numbers randomly in javascript [closed]

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));
}

Categories