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?
Related
This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
In javascript, if (true || false) results in true, but the question is whether the compiler will evaluate the second statement if the first is true.
In my case, I want to know if an array has changed, so I believe I have two options: compare the lengths of the arrays (what was and what is now) and compare if the array values are different.
I think the first option requires less work.
If (first || second) { give me deleted values, and give me added values }
No, it does not
first() || second()
function first() {
console.log('first')
return true
}
function second() {
console.log('second')
return false
}
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('')
}
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why does this index of a two dimensional array return -1 [duplicate]
(2 answers)
Closed 3 years ago.
I have simple nested array, like:
var arr = [[75.0], [65.0]] ;
and when I do:
arr.indexOf( [75.0] );
I expect 0, but I get -1, what gives?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf :
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).
There is the problem. In your example, you have an array of arrays. Comparing with === operator means that for it to evaluate to true, it has to be the same array object. Clearly it is a different object so it is not found from the array.
You need to use Array.find() instead where you can provide the testing function. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
[] is not identical to [] [duplicate]
(4 answers)
Closed 4 years ago.
In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this
const reqParam = []
if (reqParam === []) {
console.log('empty array')
} else {
console.log('string')
}
But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.
Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.
This question already has answers here:
Checking if an array has some items in coffeescript
(3 answers)
Closed 8 years ago.
I am trying to figure out if an array is empty. I tried this and it does not work.
a = []
if a == []
alert "empty"
I know I can check the length of the array but am curious why this does not work.
There is no direct empty check for arrays. You could do something like this though:
a = []
alert("empty") unless a.length