JavaScript and the proper way to format the weekdays

Learning programming is new to me, so please bear with me.

I am currently utilizing Informer, a reporting website that retrieves data from Datatel, a unidata database.

My current task involves working on a computed column that only accepts Javascript and no other language.

The objective of the column is as follows:

"If classes are scheduled for Mon, Tue, Wed, Thur, and Fri then display Mon-Fri"

In simpler terms, if classes take place every weekday, I want to condense the output to simply say "Mon-Fri" instead of listing out each individual day.

However, the code I have written is not functioning as intended. It displays "Mon-Fri" for every class, even those that do not meet on those days.

Here is the code snippet that is not yielding the desired results:

//declare variables
var mon = secmonday[1];
var tue = sectuesday[1];
var wed = secwednesday[1];
var thur = secthursday[1];
var fri = secfriday[1];
var formatDays = "";


//if monday through friday = Y (Y being the value in the database) 
//then format with a hyphen between days

if ((mon && tue && wed && thur && fri) == "Y");
{
formatDays = "Mon-Fri";
}
else
{
// if any of the days fields are empty then do not display formatDays
//instead leave it blank    
if ((mon || tue || wed || thur || fri) == null);
}
formatDays = mon + tue + wed + thur + fri;

Could you please point out what I am doing wrong? Your assistance would be greatly appreciated. Thank you!


Update: I managed to find a solution to the issue:

//define variables
var days = courseSections6_csmdaysk;
var output = "";
var formatDays = "Mon-Fri";
//removes whitespace within data
var formatBlank = days.replace(/\s+/g, '');

//if all days are present then display Mon-Fri
if (days == "M T W TH F")
{
output = formatDays;
}
else
{
output = formatBlank;
}
output

Answer №1

When handling conditions in programming, it's important to be mindful of syntax errors and use the appropriate language syntax. In this case, if you don't want to assign or end a condition after the first comparison, make sure to follow the correct format to avoid syntax errors. When working with JavaScript, always use JavaScript syntax for better results.

if( mon && tue && wed && thur && fri ) formatDays = mon+'-'+fri.

Keep in mind that this code snippet may result in something like true-true, but without more information, I am unable to provide further assistance.

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

The MVC Controller is unable to retrieve decimal values from an Ajax POST request

I am facing an issue with the POST function in my code. While string and integer values are reaching the Controller without any problem, double values are not being received on the server side. Interestingly, when I test on my local machine, everything wor ...

Limit the execution speed of a JavaScript function

My JavaScript code is set up to trigger a click event when the user scrolls past a specific element with the class .closemenu. This is meant to open and close a header menu automatically as the user scrolls through the page. The problem I'm facing is ...

An issue arises when attempting to utilize the 'push()' method to append a string to a JSON array

I am looking to append strings to my JSON file structure shown below: { "items": [] } To achieve this, I have the requirement of using the following code: var items = require("../../config/item.json"); The approach I am taking is ...

Utilizing nested JSON data with React

Hey there! I've been working on adding more levels in Json pulled from Mongo, but I'm running into an issue with accessing elements that have multiple levels of nesting. It seems like it can't read the undefined property. Is there a limit t ...

The toggle button for columns is not triggering the callback action

When working with the following JSFiddle, I noticed that the action function does not seem to fire whenever a button to select a column in the column visibility tool is selected. Check out the code snippet below for reference: $(document).ready(function( ...

Personalized validation using Bootstrap V5

Currently, I am using the default Bootstrap V5 form validator and I am interested in finding a way to create a custom parameter that must be checked for the input to be considered valid. Specifically, I want users to input their license plate, which should ...

Despite element being a grandchild, the function this.wrapperRef.current.contains(element) will return false

The issue arises when using the EditModal component with an onClickOutside event. This component includes a child element, a Material-UI Select, where clicking on a MenuItem triggers the onClickOutside event, causing the modal to close without selecting th ...

Identify the browser dimensions and implement CSS styling for all screen resolutions

I am currently facing an issue with a function that I have created to apply CSS changes to a menu based on browser resizing and different resolutions. The problem lies in the fact that my function does not seem to be correctly interpreted by the browser. W ...

Determining the value of an element by examining the clicked element

My goal is to determine the remaining balance in my cart based on the selected item. Let's say I have 3 items in my cart, and I choose one that costs $100. Previously, I stated that I had a total of $300. In my HTML code, there are elements with IDs ...

Is there a way to streamline a function that substitutes certain words?

Looking for ways to simplify my function that shortens words when the label wraps due to different screen resolutions. It seems like it could be more efficient to use arrays for long and short word pairs, but I'm not sure how to implement it. Check ou ...

Unable to retrieve information from compact JSON files

It's been 2 hours and I still can't figure this out I need help with reading my Json Data in my JavaScript File, saving it to a variable, and then printing it out. I've tried multiple solutions but nothing seems to work (I'm new to ...

a guide on using ajax to distribute retrieved data among various td elements

After the user presses a button, the data is saved into the database. Upon success, I need to retrieve the data from the database and place it in the appropriate td (in the same row as the clicked td). I've been able to successfully retrieve the data, ...

Error in Browserify Express App: Unexpected token while parsing the file

I have been attempting to browserify a javascript file. When I run the command: browserify global.js -o bundle.js An error message is returned: Error: Parsing file C:\ocquiz\public\javascripts\global.js: Unexpected token (756 ...

Dynamic Visibility Control of GridPanel Header in ExtJS

I have a GridPanel that needs to display a limited number of resources. If there are more resources available than what is currently shown, I would like the panel's header/title to indicate this by displaying text such as "more items available - displ ...

The routeLink feature is unable to display the URL dynamically

https://i.sstatic.net/iD53V.png <table class="table"> <thead> <tr> <th>Name</th> <th>Price</th> <th></th> </tr> </thead> ...

Retrieving elements from the DOM using jQuery

UPDATE: JSFIDDLE link. My goal is to synchronize elements from DESKTOP 1 to MOBILE 1 (same for #2) in the fiddle. The error is appearing in the console. Go to DESKTOP 1 and choose NEW RATE from the dropdown list. Keep the console open to observe the issu ...

Guide to initializing modules in Angular 2+ using server-side responses

I used to initialize my Angular JS application by making a server call to fetch the user's access modules. I'm struggling to do the same in Angular 2+. Here is an example using AngularJS I only used ngMockE2E for demonstration purposes. (fu ...

Express app unable to receive data from Ajax request

I'm currently in the process of developing an application with React and Express. The issue I'm facing is that when I make an Ajax request from a React component to a specific Express route, the data doesn't seem to be reaching the route han ...

Error: The XPath expression provided is invalid for use with the scrollIntoView function in Selenium

My task involves utilizing Python to extract data from a website that features a filter pane requiring scrolling. I came across a code snippet that aids in navigating through a list of elements by iterating through a loop. recentList = driver.find_element ...

Using an Ajax Post Call to send FormData leads to a Get request instead

Having trouble with sending a simple form via POST method. I load the form content using AJAX: $(function() { var arg = { "operation": "upload", "step": "0" ...