Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a piece of code which make an array full of numbers with the amount of numbers i want, but instead of this beeing static with 16 numbers i tried to change 16 to a variable but the math.floor/randome cant read it it only spits out Not a number wierdly enough.
EDIT: with 16 put in it works, but i cant use a variable (declared in the same function ofc)after i console.log the variable it shows it as a number but then my browser freezes
Is There anyone who knows how to change this
while(arr.length < pictures.length) {
var randomenumber = Math.floor((Math.random()* 16));
if(arr.indexOf(randomenumber) > -1) {
continue;
}
arr[arr.length] = randomenumber;
}
//cheat sheet
for(var i = 0; i < arr.length ; i++) {
document.write(arr[i]);
document.write("<br/>");
}
I don't see any issues if you use var length = 16 and Math.floor((Math.random() * length)).
Working snippet:
var arr = [], length = 16;
while(arr.length < length) {
var randomenumber = Math.floor((Math.random() * length));
if(arr.indexOf(randomenumber) > -1) {
continue;
}
arr[arr.length] = randomenumber;
}
//cheat sheet
for(var i = 0; i < arr.length ; i++) {
document.write(arr[i]);
document.write("<br/>");
}
You will need to parse it in integer using parseInt.
var num = 16;
var randomenumber = Math.floor((Math.random() * parseInt(num)));
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
<script type = "text/javascript">
function showPrimes(limit) {
for (let number = 2; number <= limit; ++number) {
let isPrime = true;
}
}
</script>
Above the code, I am currently working on so far. Not sure how to continue.
function getPrimes(limit) {
var sieve = [], i, j, primes = [];
for (i = 2; i <= limit; ++i) {
if (!sieve[i]) {
primes.push(i);
for (j = i << 1; j <= limit; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
console.log(getPrimes(100));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I just started learning javascript, and I want to make an online lcm hcf interactive teaching tool, but I keep getting this error pointed out below, and I can't see where it's missing
$("document").ready(function(){
function start() {
var firstNum = document.getElementById("first-num");
var secondNum = document.getElementById("second-num");
var primeList1 = [];
var primeList2 = [];
var primes = [];
var maxPrime = math.max(firstNum, secondNum) / 2 + 1;
**for (int num = 2; num < maxPrime; num++) {** <--- this line has the error
for (int i = 2; num < i; i++)
if (num % i == 0) {
break;
} else {
primes.add(num);
};
};
};
There is no int keyword in JavaScript.
You need to use var or let to declare and initialize your num and i variables
for (let num = 2; num < maxPrime; num++) {
for (let i = 2; num < i; i++)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Quiz instruction:
Code a function that takes an array of numbers as a parameter and returns the average of them as the result.
Examples: average([2,2,5,8]) returns 4.25. average([59,60,40]) returns 53.
Code so far:
function average(array) {
var denominator = array.length;
var numbers = array;
for( var i = 0; i < array.length; i++) {
var total = numbers[0] + numbers[1] + numbers[2];
}
var avg = total / denominator;
return avg;
console.log( avg );
}
average([1,2,3]);
//Returning error. Not sure where the mistake is. Also, what would the code be if you were to expand the array and still get the average, no matter the inputs in the array parameter.
//Please help. Thanks!
Here is your average function;
function average(numbers) {
let denominator = numbers.length;
let sum = 0;
for (let i = 0; i < denominator; i++) {
sum = sum + numbers[i];
}
return sum / denominator;
}
console.log(average([1, 2, 3]));
console.log(average([2, 2, 5, 8]));
console.log(average([59, 60, 40]));
let average = (array) => array.reduce((a, b) => a + b) / array.length;
console.log(average([5,10,30]));
Please refer below url for calculating sum. and then find average by dividing this sum amount with array.length
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
You can do something like that:
function average(nums) {
return nums.reduce((a, b) => (a + b)) / nums.length;
}
average([1,2,3]);
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
Ok so I'm working on learning JavaScript and I came across a Coding Challenge question which I can not solve. It gives me the outline/skeleton of the format I should use and wants me to find the odd numbers from 1-5000.
Starting with the basic function given below, write a function called sumOddNumbers that will print to the console and return the sum of all the odd numbers from 1 to 5000. Consider using a loop, and don't forget to call the function afterwards!
~ Format ~
function sumOddNumbers() {
var sum = 0;
// Your code here
console.log(sum);
return sum;
}
There is no need for a loop:
console.log(5000**2/4);
If 5000 is a dynamic input to your function, then the formula is as follows:
function sumOddNumbers(n) {
return (n + n%2)**2/4;
}
console.log(sumOddNumbers(5000));
If really it has to be done with a loop, and according to the template (which is not very nice BTW):
function sumOddNumbers() {
var sum = 0;
for (let i = 1; i <= 5000; i+=2) {
sum += i;
}
console.log(sum);
return sum;
}
sumOddNumbers();
let sum = 0;
for (let i = 1; i < 5000; i += 2) {
sum += i;
}
let sum = 0;
for (let i = 0; i < 5000; i++) {
if (i % 2 !== 0) {
sum += i;
}
}
function sumOddNumbers(max) {
let sum = 0;
for(let i = 1; i <= max; i+=2){
sum += i;
}
console.log(sum);
return sum;
}
sumOddNumbers(5000);
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 6 years ago.
Improve this question
I have an array of numbers and a value. I'd like to find the numbers around the value. The most important is the highest value but I need both.
var array = [2,4,5,8,11];
var value = 9;
The result I need should be : 8 and 11.
How can I do that?
Thanks.
Just write a simple for-loop searching for each of the values.
So for example:
function findNumbers(arr, num){
var low = num + 1; //largest number lower than num, set initial value above num
var high = num - 1; //smallest number higher than num, set initial value below num
for(var i = 0; i < arr.length; i ++){
if(arr[i] > low && arr[i] < num){
low = arr[i];
}
if(arr[i] < high && arr[i] > num){
high = arr[i];
}
}
//optional check to see if low/high exists
if(low > num){
low = -1; //not found
}
if(high < num){
high = -1; //not found
}
return [low,high];
}
and there you go. This works whether or not arr is sorted.
This help you :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var array = [2,4,5,8,11];
var value = 9;
var min,max;
fun();
function fun() {
var sortArray = array.sort(function(a,b){return a-b});
for(var i=0; i<sortArray.length;i++) {
if (sortArray[i] < value)
min = sortArray[i];
if (sortArray[i] > value)
max = sortArray[i];
}
alert(min + " , " + max);
}
</script>
</body>
</html>
Looking at your question looks like you are still learning jquery.However I am sure you will get good suggestions here.
Here is an example with your test case,I am leaving the explanation on you so that you can study each line and learn.
var myArray = [2, 4, 5, 8, 11];
var myValue = 9;
function BiggerThan(inArray) {
return inArray > myValue;
}
function LesserThan(inArray) {
return inArray < myValue;
}
var arrBiggerElements = myArray.filter(BiggerThan);
var nextElement = Math.min.apply(null, arrBiggerElements);
alert(nextElement);
var arrLesserElements = myArray.filter(LesserThan);
var prevElement = Math.max.apply(null, arrLesserElements);
alert(prevElement);