English 中文(简体)
我如何能够从我的阵列中删除甚至数字?
原标题:How can I remove even numbers from my array?

Attempting to remove all even numbers from the array. I have managed to remove some but there are still two persisting. I am unsure what to do as I have very new to coding and only know the very basics.

function findEfficientBulbs(serialNumbers) {
console.log(serialNumbers);
const efficientSerialNumbers = [];
// Below

//create a loop to go through the array
for (let i = 0; i < serialNumbers.length; i++){
    //define a varible to look through the array via loop 
    const currentBulb = serialNumbers[i]
    //create if statement to find odd numbers and has 6 digits
    // remove items containing even numbers
        if (currentBulb === 6 && currentBulb % 2 === 1 ){
            //push variable into efficient array
            efficientSerialNumbers.push(currentBulb)
        }
}

return efficientSerialNumbers;
}    

error 收到:

✕ AssertionError: expected [] to deeply equal [ 234567, 456789 ]

logs

[ 123456, 234567, 345678, 456789 ]

我不理解为什么有123456和345678人回来。

最佳回答

您目前的法典正在检查<代码> 现行Bulb是否等于6,如果是奇怪的,那么它为什么不按预期行事。 为了解决这个问题,你应当检查人数的长短是否为6岁,如果是奇怪的话。 本条更正的法典:

function findEfficientBulbs(serialNumbers) {
  console.log(serialNumbers);
  const efficientSerialNumbers = [];

  for (let i = 0; i < serialNumbers.length; i++) {
    const currentBulb = serialNumbers[i];

    if (currentBulb.toString().length === 6 && currentBulb % 2 === 1) {
      efficientSerialNumbers.push(currentBulb);
    }
  }

  return efficientSerialNumbers;
}

在本法典中, CurrentBulb.toString().length ==6,条件是该数字的长度为6位数; 当前Bulb % 2 = 1,如果其数字为奇数的话。 即便有来自阵列的6位数,这还是正确的。

问题回答

如果你想简化你的职能,你可以使用<条码>过滤<>。 只能归还甚至 t的东西。 另外,对于工作时间长短,价值也需作非计算。

function findEfficientBulbs(serialNumbers) {
  return serialNumbers.filter((e) => e % 2 > 0 && String(e).length === 6)
}  

console.log(findEfficientBulbs([123456, 234567, 345678, 456789]))

这里的其他人非常出色地解释了实现你所希望的正确方式,但我希望帮助解释你所做的事情。

问题的关键在于:

if (currentBulb === 6 && currentBulb % 2 === 1 ){

这份声明基本上说,“目前的Bulb有6份 > > > /m> 并且是一个奇数”,“ALWAYS决心false,从而总是形成一个空阵。

您的错误信息进一步说明了这一点:

✕ AssertionError: expected [] to deeply equal [ 234567, 456789 ]

我认为,导致你认为,你的法典只是一纸空文,甚至连数字都没有删除。

console.log(serialNumbers);

你印制了你的起始阵列,而不是显示所有数字的输出阵列。

您可以比较数字,这样,在100 000至99999之间,其人数要长6位数:

function findEfficientBulbs(serialNumbers) {
  return serialNumbers.filter((e) => e % 2 > 0 && e >= 10000 && e < 1000000)
}  

console.log(findEfficientBulbs([123456, 234567, 345678, 456789, 1233421, 9999]))
Cycles: 10000000 / Chrome/117
-----------------------------------------------------
Alexander      151/min  1.0x  163  163  151  169  162
Hitesh Kumar   292/min  1.9x  292  298  295  309  322
-----------------------------------------------------
https://github.com/silentmantra/benchmark

<script benchmark="10000000">

const serialNumbers = [123456, 234567, 345678, 456789, 1233421, 9999];

// @benchmark Hitesh Kumar

function findEfficientBulbs(serialNumbers) {
  const efficientSerialNumbers = [];

  for (let i = 0; i < serialNumbers.length; i++) {
    const currentBulb = serialNumbers[i];

    // Check if the number has 6 digits and is odd
    if (currentBulb.toString().length === 6 && currentBulb % 2 === 1) {
      efficientSerialNumbers.push(currentBulb);
    }
  }

  return efficientSerialNumbers;
}

// @run
findEfficientBulbs(serialNumbers) 

// @benchmark Alexander
serialNumbers.filter((e) => e % 2 > 0 && e >= 10000 && e < 1000000)
</script>
<script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

Remove currentBulb === 6 from the code and use like below,

function findEfficientBulbs(serialNumbers) {
    console.log(serialNumbers);
    const efficientSerialNumbers = [];

    // Loop through the array
    for (let i = 0; i < serialNumbers.length; i++) {
        const currentBulb = serialNumbers[i];

        // Check if the current number is odd
        if (currentBulb % 2 === 1) {
            efficientSerialNumbers.push(currentBulb);
        }
    }

    return efficientSerialNumbers;
}

const serialNumbers = [123456, 234567, 345678, 456789];
const result = findEfficientBulbs(serialNumbers);
console.log(result);
function findEfficientBulbs(serialNumbers) {
return serialNumbers.filter(el => el % 2 !== 0 && el.toString().length === 6);
}

const results = findEfficientBulbs([123456, 123458];
console.log(results);

Since you are trying to remove the numbers having a length of 6 and the even numbers from an array of numbers, the above code snippet might work well for you. Here the code is filtering out the items from the array by using the above conditions. It will return a new array without the even numbers.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签