I'm having trouble getting the if statement within my JavaScript for loop to execute. What could be causing this issue?

Currently, I have a program that is designed to create a new array from an existing array of objects by asking specific questions. However, there seems to be an issue with the recognition of the if statement within the for loop when the program runs. The resulting new array contains everything from the old array.

function verb(first, second, third, fourth, conjugation, chapter) {

    this.first = first;
    this.second = second;
    this.third = third;
    this.fourth = fourth;
    this.conjugation = conjugation;
    this.chapter = chapter;
}
var family = new Array();

family[0] = new verb("amo","amare","amavi",'amatum',1,1);
family[1] = new verb("moneo","monere","monui","monitum",2,1);
//remaining array elements...


var choose = prompt("Do you want principle parts?");

var chap_beg =   prompt("What chapter do you want to begin?");
var chap_end = prompt("What chapter do you want to end"); 

var helparray = [1,2,3,4,5,6];

var person = helparray[Math.floor(Math.random() * helparray.length)];   

var randselect  = [];

for(i=0; i<family.length; i++) {
    if(chap_beg < family[i].chapter && family[i].chapter < chap_end) {
        console.log(family[i].first);
        randselect.push(family[i]);
    } else {
        console.log("no");
    }
}

var rand = randselect[Math.floor(Math.random() * (randselect.length - 1))];

This results in:

amo
moneo
a  
debeo
do
servo
conservo
terreo
valeo
video
voco
habeo
satio
culp
 ceno
maneo
supero
tolero
audeo
neco
ago

Answer №1

Here is an example...

if( start < array[i].value <  end) {

You should update it to:

if( start < array[i].value && array[i].value <  end) {

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

Using JavaScript to create delays for a group of setTimeout functions

I have a collection of items that need to be shown at various intervals using the setTimeout() function in JavaScript. What I attempted was to create a loop where, for each item, I set up a setTimeout event individually. This is the code snippet I used to ...

Maintaining the order of subscribers during asynchronous operations can be achieved by implementing proper synchronization

In my Angular setup, there is a component that tracks changes in its route parameters. Whenever the params change, it extracts the ID and triggers a function to fetch the corresponding record using a promise. Once the promise resolves, the component update ...

The foreach loop is only able to retrieve values during the initial iteration of the array

For some reason, my foreach loop is looping through all 5 ProductionOrderID's but only returning data for the first ID. I have confirmed that the array is looping correctly based on the current result visible here: https://i.sstatic.net/KhzIC.png. Ho ...

Implementing a fixed top navigation bar with jQuery to override default CSS properties

My latest project involves creating a WordPress site, and I recently found a way to fix my navbar at the top of the page as users scroll down. Here's the code snippet I used: (function( $ ){ var navOffset = jQuery("nav").offset().top; jQu ...

How was the HTML code on this website masked so effectively?

While browsing the internet, I stumbled upon a website and decided to take a peek at its source code. DCARD You can find screenshots here Unfortunately, the content is in Chinese. It seems like this website is similar to a forum in my country. What cau ...

Troubleshooting: Why isn't my Angular directive being updated by the controller?

My directive is responsible for displaying a detail screen when a row is clicked. I am currently working on updating the encounter within this directive. Take a look at the code below: angular.module('app').directive('chatContainer', f ...

Tips for integrating v-virtual-scroll with v-table?

My Vuetify table is handling a large amount of data – 300 rows with 20 columns, some of which have calculated rowspans. To improve performance, I'm considering using the v-virtual-scroll component. I came across this sample code, which doesn't ...

Tips for assigning a personalized value to an MUI Switch when it is in the off position

I am currently utilizing the MUI Switch component to create an On-Off button. I have manually set the value as "on" and it is functioning correctly when the switch is in the true state. However, there doesn't seem to be an option to change the default ...

Adjust Camera Position in A-Frame Scene Based on Scrolling Movement

I've been struggling to find a solution for this particular scenario in Aframe. I want to create an embedded Aframe scene as the background of a webpage and have the camera move along a path as the user scrolls down the page. I've set up a scene ...

Numerals for Central Leaflet Marker

Is there a way to effectively center numbers inside markers? Here is the current situation: View Marker with Number How to Create a Marker return L.divIcon({ className: "green-icon", iconSize: [25, 41], iconAnchor: [10, 44], popupAn ...

The least number of operations to make the array strictly decreasing

You are tasked with converting an array a into a non-increasing form by making the minimum changes possible, allowing for either incrementing or decrementing the array value by 1. For example: Input: a[] = {3, 1, 2, 1} Output: 1 Explanation: By changing ...

Using JavaScript to escape a single quote within a property value of a JavaScript object

I am currently facing the challenge of dynamically splitting a JavaScript Object into HTML markup from C#.NET code behind. Once I retrieve the data, I format it into a string and create an object within that string to be displayed in the HTML markup. var ...

Populating a byte array in Java

As a part of the project I am currently working on, I am tasked with implementing an RTPpacket where I need to populate the header array of bytes with RTP header fields. //defining the size of the RTP header: static int HEADER_SIZE = 12; // bytes / ...

Difficulty accessing `evt.target.value` with `RaisedButton` in ReactJS Material UI

My goal is to update a state by submitting a value through a button click. Everything works perfectly when using the HTML input element. However, when I switch to the Material UI RaisedButton, the value isn't passed at all. Can someone help me identif ...

Sending mass text messages with a customized message for each phone number - A guide using Twilio

I have a batch of 100 to 1500 phone numbers that I need to send SMS messages to. Each message should include the recipient's name associated with the phone number in the text. How can I achieve this using Twilio? client.notify.services(notifyServiceS ...

I am encountering issues in Vue JS when using ternary expressions alongside v-if statements

Snippet:- <template> <div id="calendars-results"> <div class="vs-con-loading__container"> <div class="vs-row flex justify-between"> <h4 class="vs-row mb-2">{{ title } ...

Query a MongoDB document by its nested array of objects, and then update all objects within the array that match the query criteria

I am currently working with a collection of documents that look like this: { _id: "1", quantity: 0, array: [{name: "test1a", quantity: 10}, {name: "test1a", quantity: 1}, {name: "test1b", quantity: 10}, {name: "test1c", quantity: 1}] ...

Getting state from two child components in React can be achieved by using props to pass the

In my tabbed modal dialog, I have two image list components rendering images based on props defined in the parent. Each component manages its own array of image objects. The challenge is that I need to update or delete these images using a single save butt ...

Is it possible to showcase two modals on a single page, positioning one to the left and the other to the right using Bootstrap?

Can someone help me learn how to display two modals on the same screen, one on the left and one on the right? I am new to bootstrap and would appreciate some guidance! ...

Tips for managing back and forth navigation in react native webview component using custom components on iOS

I'm currently faced with a task that involves creating a webview component with custom back and forward buttons in React Native for IOS. My current application includes a web-view component which only displays a basic browser without any built-in con ...