Statement consistently returning true

I need assistance in verifying the accuracy of an input and moving on to the next word if it's incorrect.


"Apples",
"Bananas",
"Pears",
"Door",
"Towel",
"Computer",
];

var x = myArray[Math.floor(Math.random()*myArray.length)];

function clearbox(){
if (x = myInput.value){
    var x = myArray[Math.floor(Math.random();*myArray.length)]);
    document.getElementById('myInput').value = ''
    document.getElementById("word").innerHTML = x
} else {
    document.getElementById('myInput').value = ''   ;
}
};

It seems that the if statement is not working properly, could someone kindly assist me with this issue?

Answer №1

Your conditional statement if (x = myInput.value) needs to use the comparison operator (==) instead of the assignment operator (=). Using an assignment operator will assign the value of myInput.value to x, causing it to always evaluate as true since any value greater than 0 is considered true. To correct this, modify your function like so:

function clearbox(){
    if (x == myInput.value){
        var x = myArray[Math.floor(Math.random()*myArray.length)]);
        document.getElementById('myInput').value = ''
        document.getElementById("word").innerHTML = x
    } else {
        document.getElementById('myInput').value = ''   ;
    }

Answer №2

  1. It is recommended to use == instead of = as it will always return true (you can also consider using === for type checking):
if (x == myInput.value)
  1. There seems to be a syntax error in this line (please remove the extra ;):
var x = myArray[Math.floor(Math.random()*myArray.length)]);

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

Presented is the Shield UI JavaScript sparklines chart

I'm experimenting with the new lightweight ShieldUI Sparklines chart. I've copied the code directly from this page: Now, when I try to run it on my computer, I'm encountering an unexpected issue. I can see text, but the chart itself is not ...

Extract data from a multidimensional array using a one-dimensional array as a filter

A series of checkboxes are present on a webpage where clicking them generates an array of values called allVals. This array is dynamically updated as checkboxes are toggled on and off. Additionally, there is a multidimensional array named recordSet that ne ...

Dynamic array of pointer variables in C++

Looking to create a dynamic array of pointers to objects of the class "Wire." The variable number of pointers needs to be able to increase over time, with each pointer being assigned to an object of the Wire class. (In this case, cw is a pointer to a Wire ...

The selectedIndex attribute is failing to function properly for the mat-tab-nav-bar tabs in Angular Material

I've implemented the tab navigation code as shown below: <nav mat-tab-nav-bar [selectedIndex]="0"> <a mat-tab-link *ngFor="let link of navLinks; let i = index;" [routerLink]="link.path" routerLinkActive #rla="rou ...

Utilizing the HTML5 Pattern Attribute in Conjunction with Websockets

Being completely new to web programming, I am struggling to understand why the pattern attribute in the text field below is not validating properly due to my javascript. <form id="aForm"> <input type="text" pattern="^[ a-zA-Z0-9,#.-]+$" id="a ...

Tips for successfully transferring a concealed value within a form

<p>{{item.name}}</p> // This part is functioning properly and displays correctly <form #f="ngForm" (ngSubmit)="onSubmit(f.value)" novalidate> <div *ngIf="editMode"> <input name="name" type="hidden" ngModel val ...

What could be causing the issue with uglify not functioning properly with AngularJS content?

I've created some gulp tasks to assist in building my web project. One of the tasks involves minifying js files. Here is the task code snippet: gulp.task('minify' , function() { console.log('Copy minified js '); return gulp ...

What's the best way to ensure an endless supply of copied elements with identical classes to the clipboard?

Here is a snippet of my HTML code: <Div Class='POSTS'> <Div Class='POST'> <Div Class='CONTENT'>Text1</Div> </Div> <Div Class='POST'> <Div Class=&apos ...

Tap on the child to reveal their parent

I am working with a family tree that includes dropdown menus containing the names of parents and children. Each child has a link, and when I click on a child's link, I want their father to be displayed in the dropdown menu as the selected option. Can ...

Flickering effects in THREE.js using Frame Buffer Objects

I've encountered a strange flickering issue while working on a THREE.js scene that involves using a Frame Buffer Object. The flickering disappears when I comment out the line: mesh.material.uniforms.fboData.value = renderTargetA.texture. THREE.FBO ...

Prevent scrolling in AngularJS model popups

When loading data for the first time in a model popup, the scroll bar is not displayed inside the popup. However, after performing a search function and filtering the data, the scroll bar appears inside the model popup. How can this issue be fixed? this ...

Evolution of table size

I have a table that needs to expand smoothly when a certain row is clicked. I want to add a transition effect for a more polished look. Here's my test table: <div ng-app="app"> <table> <thead ng-controller="TestController" ...

It is impossible for me to invoke a method within a function

I am new to working with typescript and I have encountered an issue while trying to call the drawMarker() method from locateMe(). The problem seems to be arising because I am calling drawMarker from inside the .on('locationfound', function(e: any ...

Controlling multiple bx-sliders with a single pager using only JavaScript

Is there a way to control 3 sliders using the same pager? I attempted to use the following code but it did not work: <script language="javascript"> $('.mobile_left_mble,.mobile_right_mble,.text_btn').bxSlider({ //pagerCustom: '#bx- ...

Error in Node application: Cannot search for 'x' in 'y' using the 'in' operator with Express and Nunjucks

Hello everyone, I am a beginner in the world of Nunjucks/Express and Node.js. I have a routes file that is capturing the value of an input from a form field. My goal is to check if this value contains the string 'gov'. Here is what my code look ...

Invoking an AJAX function that is not inside the document.ready function

I'm having trouble populating a Google Map and here's some of the code I'm using. The ajax request doesn't seem to be working properly. When I put everything inside document.ready() as an anonymous function, it works fine. However, sinc ...

Manipulate array of observable data structure in Angular using RxJs filter function

I am working on filtering observable nested arrays in Angular using the filter function in combination with the pipe function of the RxJs library. Inquiry: I am looking to display only categories with surveys conducted on a specific date. Simplified scen ...

Having difficulty retrieving information from a factory in AngularJS

I'm encountering an issue with my db factory where the data returned from the database is coming back as 'undefined' after successfully posting it to the client. This is how my factory function is structured: uno.factory('adbFactory&a ...

Issue with MeteorJS: The template event fails to trigger when within a popover

I'm facing an issue with my code. The problem I'm encountering is that the button "viewcart" click event is not triggering when clicked. This button is located inside a popover. Does anyone have any suggestions on how to activate the button click ...

Angular ng-repeat table displaying items in alternating columns

Just diving into the world of Angular and currently working on a page that utilizes ng-repeat with a table element. The repeating function is working perfectly, as shown below: Now, I'm looking to have every other table displayed side by side before ...