This question already has answers here:
Generate unique random numbers between 1 and 100
(32 answers)
Closed 2 years ago.
this is my first question here.
I need to generate an array containing 16 random numbers and that's my solution:
var arr = [];
for (var i = 0; i < 16; i++) {
arr.push(Math.floor(Math.random() * 100) + 1);
}
The problem is that in this way it is possible that there are repeated numbers. Is there someone who can help me? Thanks in advance.
The shortest approach is to use a Set and check the wanted size of it.
let numbers = new Set,
result;
while (numbers.size < 16) numbers.add(Math.floor(Math.random() * 100) + 1);
result = [...numbers];
console.log(...result);
Use a while loop
const MAX_NUMBER = 16;
const arr = [];
do {
const randomNumber = Math.floor(Math.random() * 100) + 1
// Push if the array does not contain it
if (!arr.includes(randomNumber)) {
arr.push(randomNumber);
}
} while (arr.length < MAX_NUMBER);
console.log(arr)
This question already has answers here:
How to convert array into comma separated string in javascript [duplicate]
(3 answers)
Closed 2 years ago.
I want to add a % after each element in the array except the last. So far I have come up with this:
var array = [a, b, c];
for(var i=0; i<array.length; i++) {
var outcome += array[i] + '%';
}
Outcome:
a%b%c%
How can I fix this so the % does not appear at the end of the outcome?
You can use the Array.prototype.join method in order to get what you're after:
console.info(['a', 'b', 'c'].join('%'))
Check if the current element (value of i) is not the last element. If it's the last element don't concatenate a %, for all others concatenate with the %.
for(var i = 0; i < arr.length; i++) {
if(arr[i] < arr.length -1) {
var outcome += arr[i] + '%';
}
}
This question already has answers here:
How do I build a loop in JavaScript? [closed]
(4 answers)
Closed 4 years ago.
I have written a function that generates different numbers each time it is called. Now I want it to genarate say 50 different numbers and then push it into an array. How do I go about it? This is the code below:
function generateRandomNumbers() {
let randomArray = [];
let digits = Math.floor(Math.random() * 900000000) + 100000000;
digits = `0${digits}`;
// Last task is to push 50 random digits into the array;
return randomArray;
}
console.log(generateRandomNumbers());
You need to do something like this:
function generateRandomNumbers(howMany) {
let arr = [];
for(let i = 0; i <= howMany; i++)
{
let digits = Math.floor(Math.random() * 900000000) + 100000000;
digits = `0${digits}`;
arr.push(digits);
}
return arr;
}
let randomArray = generateRandomNumbers(50);
console.log(randomArray);
This question already has answers here:
How to find the sum of an array of numbers
(59 answers)
Closed 6 years ago.
I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:
var total = 0;
for (var i = 0; i < totalPrice.length; i++);
total += totalPrice[i];
document.getElementById("displayPrice").innerHTML = total;
But total comes out as a NaN.
This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?
https://jsfiddle.net/bgv5s9re/2/
You could use brackets
var total = 0;
for (var i = 0; i < totalPrice.length; i++){
total += totalPrice[i];
}
document.getElementById("displayPrice").innerHTML = total;
This question already has answers here:
How to randomize (shuffle) a JavaScript array?
(69 answers)
Closed 8 years ago.
My code there are no of divs here, I have to select 5 randomaly at a time
<div id="#jpId1"></div>
<div id="#jpId2"></div>
<div id="#jpId3"></div>
<div id="#jpId4"></div>
<div id="#jpId5"></div>
<div id="#jpId6"></div>
<div id="#jpId7"></div>
<div id="#jpId8"></div>
<div id="#jpId9"></div>
<div id="#jpId10"></div>
<div id="#jpId11"></div>
I want in array (r), values of id's with no repeat but the values are repeated.... Any help is appreciable ... I have to use these ids for specific purpose
var itemp = ["#jpId1", "#jpId2", "#jpId3", "#jpId4", "#jpId5", "#jpId6", "#jpId7","#jpId8", "#jpId9", "#jpId10", "#jpId11"]
for (var i = 0; i < 5; i++) {
var r = itemp[Math.floor(Math.random() * itemp.length)];
alert(r);
}
Try this (splice removes the selected element from the source array) :
var r = [];
for (var i = 0; i < 5; i++) {
r.push(itemp.splice(
Math.floor(Math.random() * itemp.length), 1
)[0]);
}
alert(r);
You can check below code:
var nums = ["#jpId1", "#jpId2", "#jpId3", "#jpId4", "#jpId5", "#jpId6", "#jpId7","#jpId8", "#jpId9", "#jpId10", "#jpId11"],
ranNums = [],
i = 5,
j = 0;
k = 10;
while (i--) {
j = Math.floor(Math.random() * (k+1));
ranNums.push(nums[j]);
nums.splice(j,1);
k--;
}
console.log(ranNums);
And you can find link here http://jsfiddle.net/8Xb5g/1/
When you get the first random value from the array use the splice method to remove the that particluar value from the array.
var random = Math.floor(Math.random() * item.length);
item[random];
Then remove that particular value from array.
item.splice(random,1);
Use this in a loop it will give you everytym new value.
Here is the code:
var itemp = ["#jpId1", "#jpId2", "#jpId3", "#jpId4", "#jpId5", "#jpId6", "#jpId7","#jpId8", "#jpId9", "#jpId10", "#jpId11"];
id_arr=Array();
for (var i = 0; i < 5; i++) {
var r = itemp[Math.floor(Math.random() * itemp.length)];
if($.inArray(r, id_arr)>-1)
{
alert ("duplicate "+r+", hence discarded");
i--;
continue;
}
else
id_arr.push(r);
alert(r);
}
console.log(id_arr)
Fiddle