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 4 years ago.
Improve this question
I need to create an array of 20 random numbers in the range from -10 to 10 inclusive and Check whether all numbers match the parameters (from -10 to 10).
here is my code:
let array = [];
for (let i = 0; i < array.length; i++) {
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
It should be more like this
let array = [];
for(let i=0; i<20; i++){
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
This will do the trick,
var array = Array(20).fill().map(() => Math.round(Math.random() * 20) - 10);
console.log(array)
Related
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 5 months ago.
Improve this question
I need a simple algorithm to find the number of occurrences of 0 between 1 in an array, for example, after performing certain operations on these arrays:
var mas1 = [0,1,0,0,1]
var mas2 = [1,0,0,0,1]
var mas3 = [0,1,0,1,0]
var mas4 = [0,0,0,0,1]
var mas4 = [1,0,1,0,1]
We have to get:
result = 2
result = 3
result = 1
result = 0
result = 2
We can have any number of 1's or 0's in an unlimited array
Any ideas?
You can continuously use indexOf to find the next 1 while accumulating the size of ranges in between consecutive ones.
let arr = [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1];
let count = 0;
for(let i = arr.indexOf(1); i != -1; ){
const next = arr.indexOf(1, i + 1);
if (next !== -1) count += next - i - 1;
i = next;
}
console.log(count);
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 10 months ago.
Improve this question
const marks = [80];
const grade = ['A', 'B', 'C', 'D', 'F'];
let sum = 0;
console.log(checkGrade(marks));
function checkGrade(array) {
for (let item of array)
sum += item;
const avg = sum / array.length;
let gradePoint = Math.floor((100 - avg) / 10);
return gradePoint % 10;
}
as Your code, the avg is = 80
and (100-avg) / 10 = 2 which is gradePoint
and you are returning gradePoint % 10
that means 2 % 10
which is returning 2.
and it is working perfectly. Where is the issue?
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 12 months ago.
Improve this question
Sorry, I dont know anything about programming, so bare with me.
I need an expression for a textlayer in adobe after effects which uses javascript. The expression needs to create a customizable list of numbers using 3 variables -
example:
x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
the output needs to be a list with each entry in a new line, in this case:
0
20
40
60
80
Hope somebody can help. Thanks
x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
let output=[]
for(let i=x;i<z;i++){
console.log(i*y)
output.push(i*y)
}
console.log(output)
let arr = [];
let x = 0;
let y = 20;
let z = 5;
for (let i = 0; i < z * y; i+= y) {
arr.push(i);
}
console.log(arr);
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 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k
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 4 years ago.
Improve this question
How do i calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * 4 ... N
Any Help on this?
Because zeros come from factors 5 and 2 being multiplied together, iterate over all numbers from 1 to the input number, adding to a cumulative count of fives and twos whenever those factors are found. Then, return the smaller of those two counts:
function zeroCount(n) {
let fives = 0;
let twos = 0;
for (let counter = 2; counter <= n; counter++) {
let n = counter;
while (n % 2 === 0) {
n /= 2;
twos++;
}
while (n % 5 === 0) {
n /= 5;
fives++;
}
}
return Math.min(fives, twos);
}
console.log(zeroCount(6)); // 720
console.log(zeroCount(10)); // 3628800
It is very simple, This will help you.
function TrailingZero(n)
{
var c = 0;
for (var i = 5; n / i >= 1; i *= 5)
c += parseInt(n / i);
return c;
}
Let me know if you need help to understand this function.