This question already has answers here:
Does every Javascript function have to return a value?
(4 answers)
JavaScript Functions , return undefined
(4 answers)
Javascript function: no return statement identical to return undefined? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to write a javascript algorithm that checks to see if an array of characters could make a given string.
I decided to go about this by writing a function that converts the array to a string so that I can call the .match() method on that string to search for the characters of the given string.
function arrStr(arr,str) {
cleanArray(arr).match(str) ? true : false
}
function cleanArray(arr){
return arr.join('')
}
When I attempt to execute this algorithm, I keep getting "undefined". Anybody have any ideas as to why? Thank you
You are not returning anything from arrStr()
function arrStr(arr,str) {
return cleanArray(arr).match(str) ? true : false
}
function cleanArray(arr){
return arr.join('')
}
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 2 years ago.
editable: params => this.isEditable(applied)(params)
this editable get boolean value by calling iseditable function by passing applied as a paramaters. but params is get passed with it. what is the meaning of this code?
Well, you only showed a small snippet, but I suspect it's something along the lines as
{// Some object is being constructed...
editable: params => this.isEditable(applied)(params)
}
Which is an ES6 "arrow function expression", which returns some other function, as the return value of calling isEditable.
//Some object is being constructed...
{
editable: function ( params ){
return this.isEditable(applied)(params)
},
// More properties being defined...
}
You can learn more about ES6 arrow function expressions here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 5 years ago.
I'm learning JS and had to come up with a function to check if a certain string is a palindrome. I managed to get it right with:
function isPalindrome(word) {
return word == word.split('').reverse().join('');
}
However, my first attempt was:
function isPalindrome(word) {
return word.split("") === (word.split("").reverse());
}
But this does not work. What this second function does is get the string and make an array out of it, then compare that to the string as an array but reversed. If I console.log() both sides I get the same array (in the case of a palindrome like "level") so why does this always evaluate to false?
This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 5 years ago.
Coming from java, learning angular/typescript: The ! negates booleans or boolean expressions. This is, according to my research, the same in javascript/typescript. But what does the following method do?
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
What is !product? Product is coming in as a method parameter and then it is asked, if product is not product, or wtf should that mean? :D
This question already has answers here:
return false the same as return?
(8 answers)
Closed 7 years ago.
I have two function for example :
function fn1(){
return false;
}
function fn2(){
return;
}
alert(fn1());
alert(fn2());
return will give you "undefined" and return false will give you boolean "false" what should i prefer and why ??
both will come out from the function but what to choose and what should not
Better as javascript standard point of you
You should return whatever you need.
If you don't need an specific answer just "return", if you want know the result of the function you must return a value (boolean or not).
This question already has answers here:
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Closed 7 years ago.
I got an unexpected return value "undefined" from function 'fnExample1' if I indent code like this :
function fnExample1() {
return
1;
}
I have made this simple example : Example jsfiddle
Thanks.
write return and 1 in one line
function fnExample1() {
return 1;
}
The reason why your code returns undefined is javascript's "semicolon insertion", where a semicolon is inserted after return, and 1 is considered another statement (which is not executed)