Javascript mix numbers randomly [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to randomize a javascript array?
Hello guys I know how to generate a random value with Math.random() in Javascript, but can you tell me how to mix numbers randomly?
For example I have numbers 1,2,3,4,5,6,7,8,9,10 how to mix it randmoly like this: 2,8,9,1... so each number should be used only once

You could do this by putting them all in an array and sort that array in a random fashion.
var nrs = [1,2,3,4,5,6,7,8,9,10];
nrs.sort(function(a,b){
return Math.floor(Math.random()*3 - 1);
});

var nums = [1,2,3,4,5,6,7,8,9,10], numsMixed = [];
while(nums.length){
numsMixed = numsMixed.concat(nums.splice((Math.random() * nums.length), 1));
}
console.log(numsMixed);

Related

Can it be that difficult to generate 10 random numbers, no repeat? [duplicate]

This question already has answers here:
Generate unique random numbers between 1 and 100
(32 answers)
Closed 2 years ago.
I feel like this should be an easy exercise:
1.- Generate 10 random numbers (0-99) and storage in an array.
2.- Numbers should not repeat.
But all the answer I get from the internet are very complicated or excessive long code.
In the code below I already generate the 10 numbers, but they keep repeating. Any ideas?? (i tried If/else but it didn't work) :(
numbers=[]
for(i=0;i<10;i++){
var oneRandomNum = Math.floor(Math.random()*100);
numbers.push(oneRandomNum);
}
console.log(numbers);
Thank you so much!!!!! :)
You can repeatedly add numbers to a Set, and stop when its size reaches 10:
const set = new Set();
while (set.size !== 10) {
set.add(Math.floor(Math.random() * 100));
}
const numbers = [...set];
console.log(numbers);

Excel formula calculations into JavaScript [duplicate]

This question already has answers here:
JavaScript exponents
(8 answers)
Closed 4 years ago.
I have a formula into my excel sheet, now I am making that formula using JavaScript. I made a code exact like written on excel sheet but in JavaScript I am getting different result, I mean wrong result.
Excel formula
= H4*((((1+H7)^H8-1)/H7)*(1+H7))
JavaScript
var contribute = 1000;
var cum_rate = 0.001666667;
var num_periods = 480;
var fvc = contribute * ((((1 + cum_rate) ^ num_periods - 1) / cum_rate) * (1 + cum_rate));
console.log(fvc);
Result of this calculations should be 735659.68 but here I am getting wrong result, can you guys help me out what I am doing wrong here?
The karat doesn't mean exponent. You need to use Math.pow(base, exp) to evaluate the expression correctly:
var fvc = contribute*( ((Math.pow( (1+cum_rate), num_periods ) - 1)/cum_rate)*(1+cum_rate) )

functional loop given a number instead of an array [duplicate]

This question already has answers here:
Tersest way to create an array of integers from 1..20 in JavaScript
(16 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 2 months ago and left it closed:
Original close reason(s) were not resolved
Say I have a number 18, instead of an array, in hand.
What is the best way to create a functional loop in JS given a number X instead of array of X elements?
I can do this:
[1,2,3].forEach(function(){
));
but if I have the number 3
I can do
for(var i = 0; i < 3; i++){
}
but I want that loop to be functional instead
If you have a number and you want to create a loop then you can use the number in limiter condition in the for loop.
for(var i = 0; i < number; i++)
Edit 1: you can use foreach on arrays only, in that case since you have a number already you can create a array of that length and then use the foreach on it.
var foo = new Array(number).fill(0);
foo.foreach()
Also another option is
var N = 18;
Array.apply(null, {length: N}).map(Number.call, Number)
result [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
Many more options available in this thread Create a JavaScript array containing 1...N
I don't understand why you want to do this. An equivalent to:
[1,2,3].forEach(function(){ ... ));
Is
var limit = n;
while (--limit) {( // Note: 0 is falsy
function(){ ... }
)(limit);}
Or if you really want to use an array structure, the following will do:
new Array(limit).fill(0).forEach(function(){...});
You might be interested in Myth of the Day: Functional Programmers Don't Use Loops.
Per this question, you can "functionally" iterate over a linear sequence relatively easily using:
Array.apply(null, Array(number)).map(function () {}).forEach(...)
Not sure what advantage this gives you versus a regular for-loop with an index, though it is a neat trick.

How to choose pseudo-random values in an array javascript [duplicate]

This question already has answers here:
Generate unique number within range (0 - X), keeping a history to prevent duplicates
(4 answers)
Closed 7 years ago.
I know how to sort through an array like this
var rand = myArray[Math.floor(Math.random() * myArray.length)];
but what I am trying to do is use this in a loop to pick values from my array that I haven't picked with this function before.
In other words, let's say my array contains apples, bananas, and oranges. i want to be able to pick all three of those out randomly, but I don't want to pick able to pick out the same one more than once.(I hope this made sense)
You can remove the item from the array, so it will not be selected again
var rand = myArray.length ? myArray.splice(Math.floor(Math.random() * myArray.length), 1)[0] : undefined;
Demo: Fiddle
Note: It will modify the original array, so if you want to keep the original array as it was you need to keep a different copy

Convert equation string to equation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running an equation with Javascript from a text field
How can I convert the following:
var n = "2x^3+3x+6";
To
var x = a number
var n = 2x^3+3x+6;
In JavaScript?
Quite hard to guess what the exact requirements and the context are, but if you want to roughly stick to the grammar demonstrated by your variable I'd suggest using a math expression parser.
Using js-Expression-eval, it could look like this:
var formula = "2*x^3+3*x+6";
var expression = Parser.parse(formula);
var result = expression.evaluate({ x: 3 });
Run the Fiddle
Should you want to have your own grammar - to leave out the * symbols for multiplication with variables, for example - you'll have to roll your own parser, for example using something like jison.
var x = a number;
var n = eval("2*Math.pow(x,3)+3*x+6")

Categories