检查一个数组,确保偶数和奇数的模式得以保持。

huangapple go评论65阅读模式
英文:

Given an array, check that the pattern of even then odd is upheld

问题

这是你提供的JavaScript代码的中文翻译:

我在这个问题上遇到了困难

> 给定一个数组请检查每个数字是否从偶数变为奇数数组中的所有数字都是正整数
> 如果有一个数字违反了这个模式请返回该数字的索引
> 如果没有任何数字违反模式请返回-1

示例数组及解决方案

//[1, 4, 5, 7, 4] // 3

//[25, 25, 25] // 1

//[4, 5, 2, 7, 4, 9] // -1

我已经解决了这个问题除了\[25, 25, 25\]数组的情况希望用JavaScript解决这个问题

虽然不是很漂亮但这是我目前的进展

```javascript
` function parity(numbers) {
   let evenIndex = []
   let oddIndex = []
   let container = []

   for (var i = 0; i < numbers.length; i+=2) {
    evenIndex.push(numbers[i])
   }

   for (var i = 1; i < numbers.length; i+=2) {
    oddIndex.push(numbers[i])
   }

   if (numbers[0] % 2 === 0) {
    for (var i = 0; i < evenIndex.length; i++) {
      if (evenIndex[i] % 2 !== 0) {
        container.push(numbers.indexOf(evenIndex[i]))
      }
    }
    for (var i = 0; i < oddIndex.length; i++) {
      if (oddIndex[i] % 2 !== 1) {
        container.push(numbers.indexOf(oddIndex[i]))
      }
    }
   }
  

   if (numbers[0] % 2 === 1) {
    for (var i = 0; i < evenIndex.length; i++) {
      if (evenIndex[i] % 2 !== 1) {
        container.push(numbers.indexOf(evenIndex[i]))
      }
    }
    for (var i = 0; i < oddIndex.length; i++) {
      if (oddIndex[i] % 2 !== 0) {
        container.push(numbers.indexOf(oddIndex[i]))
      }
    }
   }

   if (container.length === 0) {
    return -1
   }
   return Math.min(...container)
}`


<details>
<summary>英文:</summary>
I&#39;m stumped on this problem:
&gt; Given an array, check that every number switches from even to odd. All numbers in the array are positive integers.
&gt; If there is a number that breaks the pattern, return the index of that number.
&gt; If there aren&#39;t any numbers that break the pattern, return a -1
Example arrays + solutions:
//\[1, 4, 5, 7, 4\] // 3
//\[25, 25, 25\] // 1
//\[4, 5, 2, 7, 4, 9\] // -1
I have solved for this problem, except when it comes to the \[25, 25, 25\] array. Looking for this to be solved in JS.
Really not pretty, but here&#39;s what I have so far:

` function parity(numbers) {
let evenIndex = []
let oddIndex = []
let container = []

for (var i = 0; i < numbers.length; i+=2) {
evenIndex.push(numbers[i])
}

for (var i = 1; i < numbers.length; i+=2) {
oddIndex.push(numbers[i])
}

if (numbers[0] % 2 === 0) {
for (var i = 0; i < evenIndex.length; i++) {
if (evenIndex[i] % 2 !== 0) {
container.push(numbers.indexOf(evenIndex[i]))
}
}
for (var i = 0; i < oddIndex.length; i++) {
if (oddIndex[i] % 2 !== 1) {
container.push(numbers.indexOf(oddIndex[i]))
}
}
}

if (numbers[0] % 2 === 1) {
for (var i = 0; i < evenIndex.length; i++) {
if (evenIndex[i] % 2 !== 1) {
container.push(numbers.indexOf(evenIndex[i]))
}
}
for (var i = 0; i < oddIndex.length; i++) {
if (oddIndex[i] % 2 !== 0) {
container.push(numbers.indexOf(oddIndex[i]))
}
}
}

if (container.length === 0) {
return -1
}
return Math.min(...container)
}`


</details>
# 答案1
**得分**: 2
You can simply check if the parity of each element is equal to the parity of the previous element. The first index for which this is true should be returned. If the end of the array is reached, then return `-1`.
```javascript
function solve(arr) {
let x, y;
for (let i = 0; i < arr.length; i++) {
y = arr[i] % 2;
if (x === y) return i;
x = y;
}
return -1;
}
console.log(solve([1, 4, 5, 7, 4])); // 3
console.log(solve([25, 25, 25])); // 1
console.log(solve([4, 5, 2, 7, 4, 9])); // -1

TypeScript Playground

英文:

You can simply check if the parity of each element is equal to the parity of the previous element. The first index for which this is true should be returned. If the end of the array is reached, then return -1.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function solve(arr) {
let x, y;
for (let i = 0; i &lt; arr.length; i++) {
y = arr[i] % 2;
if (x === y) return i;
x = y;
}
return -1;
}
console.log(solve([1, 4, 5, 7, 4])); // 3
console.log(solve([25, 25, 25])); // 1
console.log(solve([4, 5, 2, 7, 4, 9])); // -1

<!-- end snippet -->

TypeScript Playground

huangapple
  • 本文由 发表于 2023年5月18日 08:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定