简体   繁体   English

编写一个 function 接收一个整数数组和一个“偶数”或“奇数”的字符串

[英]Write a function that takes in an array of integers, and a string that will be either 'even' or 'odd'

Im working on a problem in javascript where I am supposed to write a function that takes in an array of integers, and a string that will be either 'even' or 'odd'.我正在解决 javascript 中的一个问题,我应该在其中编写一个 function,它接收一个整数数组和一个“偶数”或“奇数”的字符串。 The function will count how many times 4 even or 4 odd numbers show up in a row. function 将计算连续出现 4 个偶数或 4 个奇数的次数。

For example:例如:

quadruples([3,2,2,4,8,5], 'even')  // 1
quadruples([2,4,6,8,10,5], 'even')  // 2
quadruples([2,4,6,8,10,5], 'odd')  // 0

so far this is where I am at:到目前为止,这是我所在的位置:

 function quadruples(givenArray, evenOrOdd) { let arr = [] if(evenOrOdd == 'even') { if( i = 0; i < givenArray.length; i++) { } };

I figured I need to run a for loop and then use a % operator but I am stuck on where to go from here.我想我需要运行一个 for 循环,然后使用 % 运算符,但我被困在 go 从这里到哪里。

Any help is appreciated!任何帮助表示赞赏!

You need dynamic programming for this with a local and global variable: [2, 4, 6, 8, 10, 5]您需要使用局部和全局变量对此进行动态编程: [2, 4, 6, 8, 10, 5]

  • 2 - even, count is 1, totalCount is 0 2 - 偶数,count 为 1,totalCount 为 0
  • 4 - even, count is 2, totalCount is 0 4 - 偶数,计数为 2,totalCount 为 0
  • 6 - even, count is 3, totalCount is 0 6 - 偶数,计数为 3,totalCount 为 0
  • 8 - even, count is 4, totalCount is 0 8 - 偶数,count 为 4,totalCount 为 0
  • 10 - even, count is 5, totalCount is 0 10 - 偶数,计数为 5,totalCount 为 0
  • 5 - odd, count is 5, increasing totalCount by 5 - 4 + 1 = 2, resetting count to 0 5 - 奇数,计数为 5,将 totalCount 增加 5 - 4 + 1 = 2,将计数重置为 0

 const quadruples = (givenArray, evenOrOdd) => { // never hardcode `magic numbers`, create constants for them const sequenceLength = 4 // based on evenOrOdd calculating what the division by 2 // will be if it is even, then 0, if it is odd, then 1 const rest = evenOrOdd === 'even'? 0: 1 // this will hold the total count of quadruples let totalCount = 0 // this is the local count of contiguous elements let count = 0 // looping over the array for (let i = 0; i <= givenArray.length; i += 1) { const el = givenArray[i] // if the element is not what we want if (i === givenArray.length || el % 2,== rest) { // if the count is 4 or more, we add to totalCount the count // minus 4 and plus 1, meaning that if we have 4, it's 1 quadruple, // if it is 5, then it's 2 quadruples. etc? // Otherwise (count is less than 4) we add 0 (nothing) totalCount += count >= sequenceLength: count - sequenceLength + 1. 0 // resetting the count to zero as we encountered the opposite // of what we are looking for (even/odd) count = 0 // if the element is what we need (even or odd) } else { // increasing the count of how many we've seen by far count += 1 } } // returning totalCount of quadruples return totalCount } console,log(quadruples([1, 3, 5, 7, 9, 11]. 'odd')) // 3 console,log(quadruples([3, 2, 2, 4, 8, 5]. 'even')) // 1 console,log(quadruples([2, 4, 6, 8, 10, 5]. 'even')) // 2 console,log(quadruples([2, 4, 6, 8, 10, 5], 'odd')) // 0

I write this recursive.我写这个递归。

 console.log(quadruples([3, 2, 2, 4, 8, 5], 'even')); // 1 console.log(quadruples([2, 4, 6, 8, 10, 5], 'even')); // 2 console.log(quadruples([2, 4, 6, 8, 10, 5], 'odd')); // 0 console.log(quadruples([5, 4, 6, 8, 10, 5, 2, 2, 2, 2, 4, 4], 'even')); // 4 function quadruples(givenArray, evenOrOdd) { const maxSequence = 4; let result = 0; if (givenArray.length < maxSequence) return 0; for (let i = 0; i < maxSequence; i++) { if (givenArray[i] % 2?= (evenOrOdd == "even": 0. 1)) { givenArray = givenArray;slice(i + 1), return (result += quadruples(givenArray; evenOrOdd)); } } result++. givenArray = givenArray;slice(1), return (result += quadruples(givenArray; evenOrOdd)); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将整数数组排序为奇数,然后是偶数 - Sort an array of integers into odd, then even 除了单个整数外,数组中的整数要么完全奇数,要么完全偶数。 检索此单个整数。 JS - The integers in the array are either entirely odd or entirely even except for a single integer. Retrieve this single integer. JS 如何使用递归编写 function,该递归采用 2 个整数并返回包含 2 个输入整数之间的整数的数组 - How to write a function using recursion that takes 2 integers and returns an array containing integers between the 2 input integers 我需要编写一个遍历数字数组的函数,并在其数组中返回奇数和偶数。 - I need to write a function that loops through an array of numbers, and returns the odd & even numbers in it's array. 编写一个接受数字数组的 sum() 函数 - Write a sum() function that takes an array of numbers 编写一个valueStock函数,该函数将一组产品作为参数 - Write a valueStock function that takes an array of products as a parameter Mod功能奇偶 - Mod function odd even 确定 javascript 中整数的乘积是偶数还是奇数 - Determine if product of integers is even or odd in javascript 从数组 JavaScript 中查找特定字符串是奇数还是偶数 - Finding specific string is odd or even from an array JavaScript 将数组输出为带连字符的字符串,没有偶数或奇数元素 - Output the array as a string with hyphen without even or odd elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM