Javascript: Incorrect comparison result

fs.readFile('./input.txt', (error, data) => {
if(error)
    console.log(error);
const input = data.toString();
const dataArray = input.split(/[\n\r ]+/);
const lastItem = dataArray.length;
let accumulator = 0;
let counter = 0;
for(let i=0; i<lastItem-1; i++) {
    let tempArray = dataArray[i];
    let splitArray = tempArray.split('x');
    let a = splitArray[0];//length
    let b = splitArray[1];//width
    let c = splitArray[2];//height
    let d = a<b? (b<c?c:b) : (a<c?c:a);
    let output = 0;
    if(d === a)
        output = (2*b + 2*c + a*b*c);
    else if(d === b)
        output = (2*a + 2*c + a*b*c);
    else
        output = (2*b + 2*a + a*b*c);   
    accumulator += output;
}})

Here is the code snippet along with input values provided for reference:

Input: 3x11x24 13x5x19 1x9x27

I've encountered issues with comparisons in my code, particularly when trying to determine the largest of three numbers. Despite expecting true results like 'console.log(a < b)' returning false for 3 and 11, I'm unsure why my logic isn't functioning as intended. Your assistance would be greatly appreciated, especially since I'm using node.js to run this code. Thank you for your help.

Answer №1

When working with your code, keep in mind that a, b, and c are strings. After splitting a string, the result is still a string, so you will need to convert it into a number.

let a = +splitArray[0];//length
let b = +splitArray[1];//width
let c = +splitArray[2];

By casting the string into a number as shown above, your comparisons should function correctly.

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

What is the process for encrypting data with javascript and decrypting it with php?

Looking for a way to encrypt data with a JavaScript function to use in a URL passed through an ajax GET request? For example, you could have encrypted data like TDjsavbuydksabjcbhgy which is equivalent to 12345: http://sample.com/mypage/TDjsavbuydksabjcbh ...

Is it possible to drag the parent element but only select the text within the child

How can I make the parent div draggable, yet keep the text in the child span selectable? <aside draggable="true" id="dragme"> This is an aside, drag me. <span id="copyme" draggable="false">But copy me!</span> </aside> A simila ...

How can I retrieve related elements within jQuery UI autocomplete?

I'm trying to enhance jqueryUI's autocomplete functionality by sending extra parameters in a more generic way. Below is a simplified version of the HTML code I'm working with: <form id='employees'> <input class='a ...

Transforming a Blob document into JSON format

I am attempting to convert a blob file into JSON format in order to transmit it via AJAX requests. Despite my efforts with the code below, I have not been successful in achieving this task. When I attempt to parse the JSONified file, I end up with a comp ...

Angular utilizes ZoneAwarePromise rather than a plain String output

I expected the giver code to return a string, but it is returning ZoneAwarePromise. Within the service: getCoveredPeriod() { let loanDetails = this.getLoanDetails().toPromise(); loanDetails.then((res: any) => { const coveredPeriodStart ...

Images are failing to show up in the iPhone design

Encountering issues with displaying images on an iPhone? You can replicate the problem by minimizing your browser window horizontally. Here is a link showcasing the problem: here. To temporarily fix this, try zooming out the browser (Ctrl+-). You can see a ...

Gather information from a customizable Bootstrap table and store it in an array

Currently, I have a bootstrap table configured with react-bootstrap-table-next, enabling users to edit cells and input their desired values. After completing the editing process, individuals can click on the "Submit" button to save the table values, which ...

Prevent ng-click functionality for markers and infowindows on an Angular map

Currently, I am utilizing angular map and have bound an ng-click event to it which triggers a dialog window to open. However, I am facing an issue where I want to disable ng-click for markers and infowindows. This problem did not arise when I was using pla ...

Understanding the Parameters for discord.js Slash Commands

I am currently working on a calculation that involves using parameters input through a slash command. While entering the parameters works without any issues, I am facing difficulty in retrieving them. The current code is resulting in an error TypeError: ...

Reactjs encountering issues loading css file

Currently, I am working on a project in Reactjs with Nextjs. To add CSS to my project, I have stored my CSS files in the 'styles' folder. In order to include them, I created a file called '_document.js' and implemented the following cod ...

The `Ext.create` function yields a constructor rather than an object as

Ext.application({ name: 'example', launch: function() { var panel = Ext.create('Ext.panel.Panel', { id:'myPanel', renderTo: Ext.getBody(), width: 400, ...

Switch up div containers with JavaScript based on the set array order of elements?

As I work on creating a list that calculates the sum of selected numbers, I encountered an issue with rearranging the items. Despite successful functionalities like adding images with names, changing languages, and performing calculations, the page keeps r ...

Is it possible to include choices in the number option for slash commands in discord.js v13?

I am currently working on creating a custom slash command that includes a numerical option. Utilizing the .addNumberOption() method for this purpose. Is there a method to restrict users from inputting numbers outside the range of 0-5 after entering the Di ...

Encountering issues with loading styles in Vue single file components

While working on my project at this link, I encountered an issue with displaying a styled modal. Despite trying to import the styles using: <style scoped> @import "../styles/modal-style.css"; </style> and even directly pasting the co ...

"Exploring the New Features in React-Native 16 Alpha 12: Updating Prop Types

Upon creating a new project, my Expo XDE issued the following warning: 1:37:35 PM Warning: checkPropTypes has been moved to a separate package. React.checkPropTypes is no longer supported and will be completely removed in React 16. Use the prop-types ...

Attempting to send PHP array to JavaScript

I have spent hours scouring the internet trying to fix my JavaScript code. My goal is to pass an array from PHP to JavaScript using JSON and then make it accessible globally for other functions. However, despite my efforts, I haven't been able to make ...

The PointerLockControls feature seems to be failing to actually lock my pointer

Uncaught TypeError: THREE.PointerLockControls is not a constructor I'm facing an issue with using firstperson controls and I can't seem to figure out the reason behind it. It's really throwing me off. const THREE = require('THREE&ap ...

Showing particular classes on an element using intersectionObserver/Scrollspy: a step-by-step guide

Here are three different sections on my Vue page. <section id="home">Home</section> <section id="about">About</section> <section id="contact">Contact</section> When I click on a Navbar Link ...

Text inside the placeholder is not displaying correctly in the React.js user interface

When passing placeholder text as a prop to the <FormField/> component from the <CreatePost/>, I encountered an issue where the placeholder text was not displaying in the form. Interestingly, when I used console.log within the <FormField/> ...

Selenium is throwing an ElementNotInteractableError indicating that the element is not able to be

Today I decided to dive into learning Selenium, but I've hit a roadblock while trying to click on an element that looks like this: <a rel="nofollow" style="" href="javascript:void(0);" time="" itemid="15 ...