This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
Can forEach in JavaScript make a return? [duplicate]
(6 answers)
Closed 1 year ago.
Here arr is array target is any number. I wants to return two numbers of arr. Sum of those number equals to value stored in target.
function twoSum(arr, target) {
var x= arr.forEach(e=>{
return arr.forEach(num=>{
if((e+num)===target){
return [e,num];
}
})
})
return x;
}
Related
This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
Curly Brackets in Arrow Functions
(3 answers)
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 1 year ago.
The following code works:
let check = checkingImages.every((item)=>item.classList.contains('matched'))
But this one doesn't:
let check = checkingImages.every((item)=>{
item.classList.contains('matched')
})
Can someone explain what's the reason for the second code not working, please?
This question already has answers here:
The invocation context (this) of the forEach function call
(5 answers)
Why "this" refers to Window in forEach in javascript? [duplicate]
(8 answers)
Using `this` in a forEach() [duplicate]
(1 answer)
Accessing this in a forEach loop results in undefined
(3 answers)
Closed 2 years ago.
In JavaScript, this depends on the function call method.
Calling General Functions
Method Call
Call constructor function
arrow function call, etc.
but in this case
class Numbers {
numberArray = [];
multiply(arr) {
arr.forEach(function(item) {
this.numberArray.push(item * item);
});
}
}
const numbers = new Numbers();
numbers.multiply([1, 2, 3]);
If you look at the fourth line in this class example,
Since arr.forEach is calling a callback function with this, I think the arr is a this but Why does this means undefined?
I don't know why it's a general function call.
This question already has answers here:
What's the difference between ++i and i++ in JavaScript [duplicate]
(6 answers)
javascript i++ vs ++i [duplicate]
(8 answers)
Closed 3 years ago.
I am trying to write a very simple script but do not understand why one syntax isn't working over the other.
The function is to simply increment any number by one.
This one does not work
function plusOne(x) {
return x++;
}
but this one does.
function plusOne(x) {
return x + 1;
}
What am I not understanding??
Increment (++)
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing
You should use
function plusOne(x) {
return ++x;
}
This question already has answers here:
Object.length undefined in javascript [duplicate]
(3 answers)
JavaScript object literal length === undefined?
(5 answers)
Closed 5 years ago.
Here is my jsfiddle
Here is my array looks like
var arr = [];
arr['alpha'] = {'a':'1','b':'1'};
arr['beta'] = {'a':'2','b':'4'};
console.log(arr)
When i take console.log(arr.length) It says 0. So i can't able to for loop this one
How can i iterate this array ?
Note :
I don't want to use jquery, i prefer only javascript
This question already has answers here:
Repeat String - Javascript [duplicate]
(30 answers)
How can I repeat strings in JavaScript? [duplicate]
(5 answers)
Closed 5 years ago.
so I am very new to js and I'm having a really hard time comprehending the language. Could anyone take 5 to look at this code and tell me why my function isn't working...? The goal is to create a function that duplicates a string by the number inputted into the function.
function repeatString(string, num) {
return string*num
}
var output=repeatString("Hello!",2);
console.log(output);
You could simply call the repeat method:
function repeatString(string, num) {
return string.repeat(num);
}