英文:
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'm stumped on this problem:
> Given an array, check that every number switches from even to odd. All numbers in the array are positive integers.
> If there is a number that breaks the pattern, return the index of that number.
> If there aren'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'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
英文:
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 < 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论