Can you please assist me by reviewing the program for any issues and guiding me on how to correct them?

<!Doctype html>     
<html>     
<title>JavaScript Tutorial</title>     
<body>     
<script language = "javascript">     
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];     
var cityToCheck = "Tucson";

if(cityToCheck === cleanCities[i]) {     
  alert("You are clean");     
} 
</script> 

</body> 

</html> 

There seems to be an issue with the code above. It's a JavaScript snippet attempting to compare the variable 'cityToCheck' to values within the 'cleanCities' array, but it might not be functioning as intended.

A similar block of code is repeated below:

<!Doctype html> 
<html> 
<title>JavaScript Tutorial</title> 
<body> 
<script language = "javascript"> 
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"]; 
var cityToCheck = "Tucson"; 
if(cityToCheck === cleanCities[i]) { 
  alert("You are clean"); 
} 
</script> 
</body> 
</html> 

The message in line 14 indicates that there could be an error or inconsistency at that point in the script.

Answer №1

The for loop with the index "i" is missing. If intentionally removed, there may be a space at the end of Tucson in your array. Utilize the trim method to remove any extra spaces.

Additionally, the language attribute should be corrected to type="text/javascript" instead of language = "javascript".

The revised code should look like this:


var cleanCities = ["Cheyenne ", "Santa Fe ", "Tucson ", "Great Falls ", "Honolulu"];
var cityToCheck = "Tucson";
for (var i = 0; i < cleanCities.length; i++) {
   if (cityToCheck === cleanCities[i].trim()) {
      alert('you are clean');
   }
}

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

Creating a grid layout by designing boxes using CSS and HTML

I'm in the process of creating a homepage and I'd like it to resemble this design: (not the best quality, but the software I used was subpar (I miss mspaint)) Originally, I used buttons which made linking and customization easy, but I encounter ...

What are some ways to ensure that my gltf loader in three.js operates synchronously?

I have a question regarding my code. I'm currently working on making the loadCone() function synchronous using async/await, but I'm facing some issues with it. import * as THREE from "three"; import { GLTFLoader } from "three/exam ...

Exporting from Blender as an OBJ file and importing it into Three.js seems to be causing issues with the mesh normals

I exported my model from blender to OBJ format and then imported it into Three.js. The normals for the wheels appear to be facing inward. As for the track, only the mesh is visible and it does not seem to be correctly mapped. Upon reimporting the OBJ fi ...

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 ...

Navigate through a webpage using anchor links without the hash symbol in the URL

My goal is to smoothly navigate through a page using the anchor tag feature. My current method involves: <a href="#div1">Link1</a> <div id='div1'>link1 points me!!</div> Whenever I click on Link1, the page effectively ...

Utilize Angular's $location to add or update the current URL parameter

I'm working on a function for handling multiple parameters in the URL when clicking an element. Specifically, I need to check if parameter pthree exists, update it to a new value without duplicating, and if it doesn't exist, append it to the curr ...

Steps for including an animation class in a div

Is it possible to add the sweep to top blue color animation to the red box using a set-timeout function instead of hover? I tried using the following code but it doesn't seem to be working. Can you help me troubleshoot? setTimeout(function() { $( ...

Is it possible to set a minimum width for browser resizing?

https://i.sstatic.net/0qhjT.png Looking at the image provided, I'm doing a comparison of the minimum resizable widths between my website and GitHub's. Is there a way to enforce a minimum width limit on my website similar to GitHub's? I&apo ...

Tips for aligning a div vertically at the bottom with overflow auto

I have some HTML code that looks like this: <div class="ex"> <div class="ex0"> <div class="ex1"> <div class="ex2"> .... </div> <div class="ex3"> .... </div> </div> </div> </div> ex0 is pos ...

Explore the steps for setting up automatic reconnection in socket.io

When establishing a socket.io connection, I use the following code: var socket = new io.connect('http://localhost:8181', { 'reconnect': true, 'reconnection delay': 500, 'max reconnection attempts': 50 }) ...

Timing problem in AngularJS service when sharing data with components on a single page

I am encountering an issue while using multiple components on a composite page and trying to reference data from a common service. The main problem arises when I attempt to create the object in the service within the first component and then access it in t ...

How can I make dragenter event only trigger on the parent element and not its children?

I'm attempting to implement a drop-zone for files in my UI that functions similarly to Google Images - where dragging a file onto the window triggers the drop-zone. However, I'm encountering an issue where the dragenter and dragleave events are b ...

What causes the _.sum() function in lodash to not work with objects in Vuejs?

I've encountered an issue where using Vuejs and Lodash in conjunction with a computed property that calculates the sum of a property in a collection results in unexpected behavior. Instead of summing the values, it seems to concatenate the string [obj ...

The jQuery script version 3.5.1 encountered an error at line 4055 where DataTables was unable to access the property "aDataSort" due to it

Hey there, I'm currently facing a small challenge while trying to incorporate Datatables within a bootstrap modal to display the results of a SQL Server query. The main requirement is to provide the option to export the data as an Excel file or in oth ...

Tips for continuously running a loop function until retrieving a value from an API within a cypress project

Need help looping a function to retrieve the value from an API in my Cypress project. The goal is to call the API multiple times until we receive the desired value. let otpValue = ''; const loopFunc = () => { cy.request({ method: &ap ...

Whenever a dependency is installed, npm install continues to display on another device instantaneously

One issue I'm encountering is that when I use npm install --save to install a dependency, it works on my end but not for others who pull the code from Git. They end up having to run npm install to get the dependency installed. What command can I use t ...

Error: The value of 'v4' property is not defined and cannot be read

I am encountering an issue with Vue.js: vue.esm.js?efeb:628 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'v4' of undefined" found in ---> <AddTodo> at src/components/AddTodo.vue <App> at src/Ap ...

Unable to present information retrieved from REST API, despite data being provided by the server

I'm attempting to make a REST call using a token in the header to retrieve information. To include the required header token, my code is structured as follows within my restClient.js, app.js, and users.js files. //restClient.js import { jsonServerR ...

Issue with implementing custom fonts using @font-face

I've added custom @font-face styling to my CSS using a downloaded font, but it doesn't seem to be working. Below is my CSS code: @font-face { font-family: "Quicksand"; src: url("fonts/Quicksand/Quicksand-Light.otf") format("opentype"); } The f ...

"Ascend beyond the constraints of fixed measurements with dynamic

My JQuery image fader has a width of 100% and height set to auto. I'm thrilled that it works perfectly on the iPhone as well. Now, I am facing a challenge while trying to make content "float" after my JQuery slider. The problem arises because I use ...