what is the problem of this code? it's showing false. this should be true.
function isSpecialArray(arr) {
for(i=0; i<arr.length; i++){
return ((arr[i % 2 == 0]) % 2 == 0) && ((arr[i % 2 !==0]) % 2 !== 0)
}
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) // false??
what is the problem of this code? it's showing false. this should be true.
function isSpecialArray(arr) {
for(i=0; i<arr.length; i++){
return ((arr[i % 2 == 0]) % 2 == 0) && ((arr[i % 2 !==0]) % 2 !== 0)
}
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) // false??
Share
Improve this question
edited Aug 19, 2021 at 14:53
Pointy
414k62 gold badges595 silver badges629 bronze badges
asked Aug 19, 2021 at 14:52
Surya YadavSurya Yadav
531 silver badge5 bronze badges
4
-
6
Your
return
statement in thefor
loop means that your function will return after examining only the first element of the array;return
exits the function immediately. – Pointy Commented Aug 19, 2021 at 14:54 - Any suggestion to make the loop worthy to examine each and every element , apart from every method of array ? – Surya Yadav Commented Aug 19, 2021 at 15:00
-
Use an
if
andreturn false
on an invalid entry in the loop. After the loopreturn true
. – Andreas Commented Aug 19, 2021 at 15:00 -
1
You could do it in one line and save unnecessary loops if you use
Array.prototype.every()
:function isSpecial(arr) { return arr.every((item, index) => item % 2 === index % 2); }
– secan Commented Aug 19, 2021 at 15:01
7 Answers
Reset to default 3You can simplify your code and avoid unnecessary looping as soon as the first element breaking the rule is found.
Using a for
loop
function isSpecial(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 !== i % 2) return false
}
return true;
}
Using Array.prototype.every
function isSpecial(arr) {
return arr.every((item, index) => item % 2 === index % 2);
}
Your return ((arr[i % 2 == 0]) % 2 == 0) && ((arr[i % 2 !==0]) % 2 !== 0)
syntax is wrong. Hope the below function meets your requirement.
Logic
- Loop through the array, check each node for "special" condition and store it in
isNodeSpecial
- "special" condition means, even index has even number and odd index have odd number.
- Initialize a variable outside the loop which holds the
isSpecial
status of array. - Update the isSpecial varaible as logical and of
isSpecial
andisNodeSpecial
. - Whenever a single node violates the condition the
isSpecial
is set to false and loop exits.
function isSpecialArray(arr) {
let isSpecial = true;
for (i = 0; i < arr.length && isSpecial; i++) {
const isNodeSpecial = (i % 2 === 0) ? arr[i] % 2 === 0 : arr[i] % 2 === 1;
isSpecial = isSpecial && isNodeSpecial;
}
return isSpecial;
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])); // true
console.log(isSpecialArray([2, 7, 4, 10, 6, 1, 6, 3]));// false
You can check if any element does not satisfy the condition and immediately return false
. If all elements satisfy the condition, then return true
. That is best solution when it es to performance. You can change your code like this:
function isSpecialArray(arr) {
for (i = 0; i < arr.length; i++) {
if (arr[i] % 2 !== i % 2) return false;
}
return true;
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) //true
console.log(isSpecialArray([1, 7, 4, 9, 6, 1, 6, 3])) //false
what you are doing is wrong because your code will return right after first execution of loop, the easiest thing you can do is to put an if condition inside your loop and check whether your odd index element is odd or even index element is even and return false if your element is wrong and keep checking for the whole array and return true outside the loop if array is special.
function isSpecialArray(arr) {
for(i=0; i<arr.length; i++){
if(!((i%2==0 && arr[i]%2==0) || (i%2==1 && arr[i]%2==1))) {
return false;
}
}
return true;
}
Thankyou
arr[i % 2 == 0] //returns undefined, b/c you are trying to do arr[true]
undefined % 2 //returns NaN
NaN == 0 //returns false, so you always get false
It is also true what the others are saying about the loop breaking right away, but that is not the main problem.
Try this instead:
function isSpecial(element, index) {
if(index%2 == 0) {
return element%2 == 0
}
else {
return element%2 != 0
}
}
function isSpecialArray(arr) {
return arr.every(isSpecial);
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) // true
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 4])) // false
simply do direct way :
const isSpecialArray = arr => arr.every((v,i)=>i%2===v%2)
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3])) // true
console.log(isSpecialArray([1, 7, 4, 9, 6, 1, 6, 3])) // false
same idea with array.reduce():
const isSpecialArray = arr =>
arr.reduce((r,v,i)=>r && ((i%2) === (v%2)), true)
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]))
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 4]))
Here's the sweet and simple way to solve this(python)
def is_special_array(list):
res=[]
for i in range(0,len(list),2):
if (list[i]%2==0) and (list[i+1]%2!=0):
res.append(True)
else:
res.append(False)
return all(res)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744409893a4572823.html
评论列表(0条)