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 3 months ago.
Improve this question
I just started to learn JavaScript. I have this script which should generate composite numbers in my separate HTML file, but I don't really understand why some parts are highlighted red when I moved script to .js file.
So my question is why there are some parts that are highlighted and how I'm suppose to write it correctly? I need to use strict in this script. I know that when using strict mode I need to declare variables, objects etc.
since you are working in a javascript file.
remove the script tags
you only need script tags in other file types like html etc
function checkComposite(num) {
var arr = [];
if( var num == 1 ) {
return false;
}
else if ( var num == 2) {
return false;
}
for (var x = 2; x < num; x++) {
if(num % x == 0) {
return true;
}
}
return false;
}
function compositeNumbers() {
num = Number(document.getElementsById('number').value);
for(var j = 1; j < num; j++) {
if(checkComposite(j)) {
arr.push(j);
}
}
document.getElementsById('result').innerHTML = arr;
}
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 1 year ago.
Improve this question
I have example of javascript array as below.
var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4...];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4...];
I would to create function to detect sign change and return with position, e.g. ['index','num1','num2'] For example.
function getSignChange(exampleArr1){
...
return someArray;
}
return shoud be
[
[2,1,-2], //index = 2 change from 1 to -2
[5,-7,5], //index = 5 change from -7 to 5
[8,2,-3], //index = 8 change from 2 to -3
]
Any advice or guidance on this would be greatly appreciated, Thanks.
Maybe this will help you:
var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4];
function getSignChange(arr){
let positive = arr[0] >= 0;
return arr.map((item, index) => {
if (positive && item < 0 || !positive && item >= 0) {
positive = arr[index] >= 0
return [index-1, arr[index-1], item]
}
}).filter(x => x != null);
}
console.log(getSignChange(exampleArr1));
console.log(getSignChange(exampleArr2));
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
I'm using Eloquent JavaScript to learn JavaScript. I am a beginner and I would like to understand how this exercise works, it is about recursion and relating in to the remainder operator. I don't understand the comparison and I really would love to know how it works.
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(50)) //true;
console.log(isEven(75)) //false;
console.log(isEven(-1)) //false;
I tried testing -2 in the log and it prints true, why does it do that?
I don't fully understand recursion or JavaScript that much, I would like it if this example is explained to me like I am 5.
Labeling the different decisions:
function isEven (n) {
if (n == 0) return true; // 1
else if (n == 1) return false; // 2
else if (n < 0) return isEven(-n); // 3
else return isEven(n - 2); // 4
}
when you call isEven(-2) it then calls isEven(2) according to // 3 which then calls calls isEven(0) according to // 4 which then returns true according to // 1
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
This is my code. I will explain everything step by step :
myobj ={ month: 1, total: 2 }{ month: 3, total: 1 }
newArray = [];
for(i=0;i<=5;i++){
$.each(myobj.function(k,v){
if(i==v.month){
newArray.push(v.month)
}else{
newArray.push(0)
}
})
}
After what I am getting is : 1,0,0,0,0,3,0,0,0,0
expected output : 1,0,3,0,0
I don't know what I am missing here. Can anyone please help me related this? I am stuck here
Your inner loop is pushing onto the new array for every item in the array, not just if the desired month is found.
Don't use an inner loop. Use find() to find the matching month, and push 0 if you don't find it.
for (i = 1; i <= 5; i++) {
if (myobj.find(el => el.month == i)) {
newArray.push(i);
} else {
newArray.push(0);
}
}
if you want to push the totals instead of the months, assign the result of find() to a variable so you can get the total from it.
for (i = 1; i <= 5; i++) {
var found = myobj.find(el => el.month == i);
newArray.push(found ? found.total : 0);
}
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 8 years ago.
Improve this question
Hi I need a probability in javascript that is a function and returns X 1 in every 2 times and Y 1 in every 30.2 times. The rest of the time the function should returns Z.
The function should be able to be called many times in a row.
This is what I have came up with so far
probability(){
var randomNumber = random() * 100;
if (randomNumber > 50) {
x()
}
else if (randomNumber > 3.31125827815 ) {
y()
}
else {
z()
}
}
Currently, your function will call y() if randomNumber is between 3.3 and 50. You probably want < instead of >
else if (randomNumber < 3.31125827815 ) {
It might make it a bit more readable to sort the intervals in ascending order:
function probability(){
var r = random();
if (r < 1/30.2){
y();
}else if(r < 0.5){
z();
}else{
x();
}
}