If it doesn't function properly in JavaScript

The result from the 'If' statement is always the last one shown.

Take a look at this code snippet:

function calc(on, ep, am, ek, b1, b2, b3, b4) {
  var sum, mo;
  b1 = parseInt(b1);
  b2 = parseInt(b2);
  b3 = parseInt(b3);
  b4 = parseInt(b4);
  sum = b1 + b2 + b3 + b4;
  mo = sum / 4;
  if (mo < 5) {
    x = "Not Good";
  }
  if (mo < 6.5 && mo > 4.99) {
    x = "Good";
  }
  if (mo < 8.5 && mo > 6.49) {
    x = "Really Good";
  }
  if (mo > 8.49) {
    x = "Perfect";
  }
  alert("O " + on + " " + ep + " " + am + " who is in " + ek + "th semester had averaged " + mo + " " + x);
}
I'm struggling to make it work with 'else if', but I need it to function as it currently does.

Answer №1

When dealing with JavaScript floating point literals, it is important to note that the decimal point is represented by a . character (U+002E : FULL STOP) and not by a , character (U+002C : COMMA) like in the case of the comma operator.

Answer №2

To clarify, you should use . instead of ,. Your expression

mo < 6,5 && mo > 4,99
will always result in 99, which is considered truthy. Therefore, the actual value of mo doesn't affect the outcome.

var mo = 10;
console.log( (mo < 6,5 && mo > 4,99) );

For further details on the comma operator, refer to MDN: Comma Operator:

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

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

Is it possible to include three sorting states in jQuery DataTables: ASC, DESC, and NO_SORT?

When clicking on a column header in jQuery DataTables, the sorting order toggles between ascending (Down Arrow) and descending (Up Arrow). However, I am looking to customize this behavior: 1st-click ascending 2nd-click descending 3rd-click no-sorting 4th- ...

Accept only requests from my Chrome extension

I have successfully set up a NodeJS server with Express on DigitalOcean. My Chrome extension is able to make GET calls to the server without any issues. However, I am looking to enhance the security of the middleware below: // Add headers app.use(function ...

Updating a select menu with AJAX without the need for a <div> tag within the form

Utilizing an ajax call to dynamically update a select menu is a common practice. The ajax call fetches the options from a separate .php file and populates the select menu accordingly. Below is the code snippet for inserting the dynamic content using JavaS ...

Choose and Send pictures through a Form with JavaScript

Currently, I am exploring different methods for allowing users to submit a form with the images they have selected. My idea is to present users with a set of images from which they can choose multiple options. Potential Solution 1: One approach could invo ...

Implementing hover intent functionality in Angular 2+

Struggling to incorporate hover intent in Angular 2. Any advice or suggestions would be greatly appreciated. ...

Unable to access the 'session' property because it is undefined - Administration of Session Cookie in Firebase Authentication-generated cookie

I'm encountering an issue where, even after setting a Firebase session cookie, I face difficulty accessing it in a secure endpoint due to the fact that req doesn't retrieve the cookie. Following the instructions outlined in a Firebase tutorial t ...

Using Promise.all to loop through generators is not a supported operation

Exploring the world of Javascript generators and promises, it becomes clear that they work well together. To effectively navigate through a coroutine of promises using Promise.coroutine from the Bluebird library simplifies executing promises in the correct ...

Send binary information using Prototype Ajax request

Currently, I am utilizing Prototype to send a POST request, and within the postdata are numerous fields. One of these fields contains binary data from a file, such as an Excel spreadsheet chosen by the user for upload. To retrieve the contents of the file ...

Updating the video tag's source attribute using JSON information

I am in the process of creating a unique video player using HTML and Javascript that will sequentially play a set of videos until all 6 have been displayed. The URLs for these videos are stored within an array that is mapped in a .json file named clips.jso ...

Obtaining the name of the image that has been uploaded using jQuery

//Image upload function $(function() { $(":file").change(function() { if (this.files && this.files[0]) { var reader = new FileReader(); reader.onload = imageIsLoaded; reader.readAsDataURL(this.files[0]); } }); }); function im ...

Passing data between Vue.js components effortlessly without relying on events

At the moment, I am utilizing Laravel Nova. It's worth noting that it operates using vue.js. I've created a personalized vue component which requires the ability to modify data inside another component. This specific component is located in the ...

Is it possible for me to hand over control to a child process and retrieve the result in Node.js?

Recently, I encountered an issue where multiple simultaneous GET requests to my Node.js server caused it to become overwhelmed and unresponsive, leading to timeouts for clients (503, service unavailable). Upon thorough performance analysis, I discovered t ...

Is it possible to utilize setIn for establishing a fresh index on an array that is currently empty?

Having trouble using Immutable in this specific structure: myMap = Map({a: [], b: []}); myMap.setIn(['a', 0], 'v'); An exception is being thrown when trying to do this immutable.js Error: invalid keyPath at invariant (immutable. ...

Various gulp origins and destinations

I am attempting to create the following directory structure -- src |__ app |__ x.ts |__ test |__ y.ts -- build |__ app |__ js |__ test |__ js My goal is to have my generated js files inside buil ...

Utilizing jQuery for adding/removing classes, however, elements are not refreshed to show changes

I am dealing with a situation involving two elements, similar to two buttons placed side by side. I am dynamically toggling the class "focusd" to alter the highlighted effect. However, I have noticed a peculiar issue where the changes are not always proper ...

Storing ajax data into a variable seems to be a challenge for me

I am facing an issue with my ajax call where I am receiving the data correctly, but I am unable to assign it to a local variable named item_price. The data that I am receiving can either be 100.00 or 115.25. Below is the snippet of my ajax code: $.ajax({ ...

Struggling with UI-Grid's single filter feature when dealing with intricate data structures?

I'm currently working with UI-Grid and facing a challenge while applying a filter to some complex data using their single filter example. Initially, everything runs smoothly when I use simple selectors. However, as soon as I attempt to delve one level ...

React error due to setting div scroll height on component mount

In my code, there is a special div/container that contains multiple <p/> tags. The container has a reference called divRef, which is initially set to null using the useRef function. <div ref={divRef} style={styles.messageListContainer}> ...

Encountered an Error: UpdateCommand is not a valid constructor - AWS SDK v3 when running unit tests with Jest in Node.js

Every time the unit test reaches the point where it calls the UpdateCommand from @aws-sdk/lib-dynamodb, I encounter an error saying "TypeError: UpdateCommand is not a constructor". I suspect that there might be an issue with the mock. I'm unsure of ho ...

Discover how to extract a whole number (and *exclusively* a whole number) using JavaScript

Is it possible to convert a string to a number in a way that only integers produce a valid result? func('17') = 17; func('17.25') = NaN func(' 17') = NaN func('17test') = NaN func('') = NaN func('1e2& ...