What is the logic behind the code returning the odd number in the array?

Can anyone clarify why the result of the following code is [1,3]?

[1,3,6].filter( item => item % 2)

I anticipated getting the even numbers from the array.

Thank you for your help!

Answer №1

An analysis is done to confirm the validity of a boolean statement, where 3 divided by 2 gives a remainder of 1, resulting in a true value.

For proper filtration, use item % 2 == 0

Answer №2

Numbers that can be evenly divided by 2 are considered even numbers.

The modulus operator % provides the remainder when it is applied to a number. For instance,

const num = 6;
const result = num % 4; // yields 2

Therefore, if % is used with 2, a number is classified as even if the remainder is zero. If not, then the number is considered odd.

// program to determine if a number is odd or even
// user input required
const num = prompt("Please enter a number: ");

//checking for an **even** number
if(num % 2 == 0) {
    console.log("This number is even.");
}

// code for **odd** number
else {
    console.log("This number is odd.");
}

Source :

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract Specific Values via Query for List or Text Array

I'm dealing with a dataset that contains subsets of data; 1 clueweb09-en0003-55-31884 0 1 clueweb09-en0006-21-20387 0 1 clueweb09-enwp01-75-20596 1 1 clueweb09-enwp00-64-03709 1 1 clueweb09-en0005-76-03988 0 2 clueweb09-en0011-28-14585 0 2 clueweb09- ...

Choose a single conditional element by using the .each method in JavaScript

I am looking to choose just one article that meets a specific condition. In my code, I am using hourDiff==0 as the condition to be true. I am using the 'each' function to loop through the articles. I have set display:none for all articles in the ...

Having trouble sending files from AngularJS to ExpressJs

Currently utilizing the angular-file-upload packages for file uploads. When triggering item.upload(), it reports a successful upload, but the req.body appears to be empty. Assistance needed! The following is the angular code snippet: var uploader = $scop ...

Discover the process of transforming jQuery code into a plugin, along with essential plugin development strategies

I have created the following jQuery script and I have some inquiries: What programming pattern would you suggest for developing a jQuery plugin? Could you guide me on how to transform my code into a plugin using $.fn.function, specifically in terms of ut ...

Go back to the top by clicking on the image

Can you help me with a quick query? Is it feasible to automatically scroll back to the top after clicking on an image that serves as a reference to jQuery content? For instance, if I select an image in the "Portfolio" section of , I would like to be tak ...

Display the children of a node and conceal the parent node when the parent node is clicked

I'm currently working on creating a unique feature that is reminiscent of a jQuery tree. Instead of allowing nodes to expand and collapse under their parent, I want the div to only display the children without showing the parent at the same time. The ...

What is the best way to set Github secrets as values within a JSON file?

Utilizing Cypress.io for automating my tests and incorporating it into CI/CD using Github Actions. Within the configuration file cypress.json, I have nested env values structured as follows: { "baseUrl": "<url-to-login>", &quo ...

Vuetify's personalized time selection tool failing to update data model and issuing an error message

For a while now, I've been grappling with an issue. I decided to create a custom component with a time picker in Vuetify. The Vue.js docs stated that for custom model components, you need to emit the input like this: <input v-bind:value= ...

Textbox value disappears after being updated

When I click on the edit link (name), the value from the database is displayed as a textbox. However, when I update another normal textbox (age), the value in the edit link textbox disappears. Strangely, if I input a new value into the edit link textbox, i ...

Each time guildMemberAdd is triggered in Discord.js, it may not run consistently

At times, I am left baffled by the inconsistency in behavior of this code. Sometimes it works like a charm, while other times it refuses to add people for hours on end, only to randomly start working again. Any suggestions on how I can resolve this issue? ...

What is the quickest way to increase value in the DOM?

Is there a more efficient method for incrementing a DOM value that you know of? let progress = +document.getElementById("progress").innerHTML; progress++; document.getElementById("progress").innerHTML = progress; Perhaps something like t ...

Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despit ...

Retrieving a nested array from JSON data

Here is the JSON data I have, stored in a variable called $onceBooking. I am trying to extract the booking_slots from each individual booking. Below is the code snippet I wrote: for ($i = 0; $i < count($onceBooking); $i++) { if (count($onceBookin ...

Tips on submitting an HTML form, utilizing ajax for processing, and showcasing PHP variable within a DIV on the current page

After struggling for over 6 hours, I've finally mustered up the courage to ask this question. My eyes are practically crossed from all the effort! Despite having some experience with PHP and HTML, Ajax and jQuery are completely new territories for me ...

Encountered an issue while loading a pretrained JSON model in a JavaScript script within a locally hosted

At the moment, I am using TensorFlow in Python to train a model and saving it as model.json along with the BIN file in a folder named models. My goal is to develop a web application that can load this pre-trained model for prediction. However, I have been ...

The CORS Policy error message "The 'Access-Control-Allow-Origin' header is missing on the requested resource" in Next.js

Encountered an issue with CORS Policy error while attempting to redirect to a different domain outside of the project. For example, trying to navigate to https://www.google.com through a button click or before certain pages load. The redirection was handl ...

What is the solution to incorporating a scrollbar in a popup when I increase the zoom level?

When trying to add a scrollbar to the popup, I am facing an issue where the scrollbar does not appear when zoomed in to 75% or 100%. It works fine for fit screen but not on zoom. Can anyone help me identify what is wrong with my code and suggest how to han ...

Leverage the power of React in tandem with Express

My web site is being created using the Express framework on NodeJS (hosted on Heroku) and I'm utilizing the React framework to build my components. In my project, I have multiple HTML files containing div elements along with React components that can ...

Utilizing a dropdown list in HTML to dynamically change images

On my HTML page, I have implemented a dropdown list box. My objective is to change an image and update a label based on the selection made from the dropdown list box. I already have an array called 'temp' which represents the number of items. The ...

Navigating within fixed-positioned divs

I'm attempting to create something similar to the layout seen on: The desired effect is to have fixed content on the right side, with only the left side scrolling up to a certain point before the entire page scrolls normally. However, in my implement ...