The JavaScript eval function is unable to carry out operations on two numbers

alert('The outcome of evaluating ${num1} ${operator} ${num2} is ${eval(`${num1} ${operator} ${num2}`)}'); 

Although I am using the eval() function to perform operations on two numbers (where num1 and num2 are numerical values and operator can be either +, -, *, or /), it seems like there is an error in my code. Can anyone spot the mistake?

Answer №1

Consider trying this:

alert(`Your outcome is ${eval(`${number1} ${operation} ${number2}`)}`); 

Using backticks instead of single quotes with template literals is a good practice. However, it's important to be careful when using eval() as it can run any code and could lead to security vulnerabilities if paired with user input.

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

React and Redux encounter an issue where selecting a Select option only works on the second attempt, not the first

I am currently working on a React/Redux CRUD form that can be found here. ISSUE RESOLVED: I have encountered an issue where the state should be updated by Redux after making an API call instead of using this.setState. The concept is simple: when a user s ...

Altering the text and functionality of buttons with jQuery

A JavaScript method I have is designed to change based on the state of a button, which is determined by using indexOf("..some text.."). $('#add1').click(function(){ if($(this).text().indexOf("Add Me!")) { $.ajax({ type: & ...

Designs for an HTML5 Cheeseburger navigation interface

I've been struggling to implement a functional and visually appealing hamburger menu on my website. The challenge lies in integrating the menu seamlessly into the page rather than having it just pop up abruptly. Despite trying various webkit tools, I ...

Retrieve Data from MySQL Database Using ExpressJS Pool Connection

I am relatively new to ExpressJS. I am encountering an issue where console.log is working fine and I can see the data in the terminal when I make a call. However, the data is not being returned as a response. Even after using console.log and return stateme ...

Explore nested arrays and retrieve objects that have corresponding categories

Working with react+ nextJS and fetching static objects, specifically "posts". The aim is to develop a "related posts" feature for each post that retrieves three posts containing at least one of the categories. Below is an excerpt simplified for clarity: co ...

Techniques for transferring array values to a function in JavaScript

I am seeking guidance on how to pass values in an array to the ajax function in JavaScript. The goal is to call the function in JavaScript for each ID present in the array. I am looking for a way to pass each ID in the array and then call the function ac ...

Encountering an issue while attempting to make changes and test a copied GitHub library - npm ERROR! version cannot be located

I'm new to the world of Github forking and pull requests. My goal is to fork a repository, make some changes, and test them out on a project before submitting a pull request. I've already forked the repository and made modifications, but I' ...

Priority is given to strings over numbers

Here's some code I'm working with: <tbody> <tr> <td class="float-left"> <!-- {{selectedTemplat?.modifiedAt | da ...

Creating a conditional interface based on props in TypeScript: A step-by-step guide

What is the most effective way to implement conditional props in a component that can be either a view or a button based on certain props? Let's take a look at an example called CountdownButtonI: class CountDownButton extends Component<CountdownBut ...

Automatically populate a dropdown list based on the selection made in another dropdown menu

I'm populating a second textbox based on the input of the first textbox in auto.jsp. Now, I want to automatically populate a combo box as well. How can I achieve this? Specifically, I want to autofill the second combo box based on the selection made i ...

What is the best method to append to the URL when a click event occurs on an HTML submit button?

Trying to access a web service with the following URL: http://servername/webapp/ws/invoices/{invoiceid} Attempting to use GET method by: <form method="GET" action="/ws/invoices"> Invoice Id: <input type="text" name="invoiceid"/> <br /> ...

Guide on sending a message to a specific channel using Discord.js version 13 with TypeScript

After recently diving into TypeScript and seeing that Discord.js has made the move to v13, I have encountered an issue with sending messages to a specific channel using a Channel ID. Below is the code snippet I am currently using: // Define Channel ID cons ...

The ngHide directive is only functional when used in conjunction with the ngRoute module, but unfortunately

Upon logging into my app, I want the login and signup buttons to disappear from the navigation bar. To achieve this, I am utilizing the ng-hide directive. This directive is triggered when the login is successful and a token is received from the server, whi ...

Is the see-through sticky header going to overlay the main banner?

I recently completed developing a client's website using Squarespace. Now, I am looking to design a header that: 1) Initially has a transparent background upon the user's arrival on the page 2) Transitions to a solid background color and sticks ...

Animating toggled classes in React can be achieved by utilizing keyframe animations or

I am looking to implement a simple transition/animation using React and CSS. The concept is to have an image that, when clicked, toggles a new section below with a nice transition effect. This is my React code: const [show, setShow] = useState(false); & ...

Switch between GeoJSON layers by using an HTML button within Mapbox GL JS, instead of relying on traditional links

I am currently developing a web map that requires toggling two GeoJSON layers on and off. In the past, I used Mapbox JS to accomplish this task by adding and removing layers with a custom HTML button click. However, I am facing some challenges in achieving ...

Pause the initial ajax data loading in Tabulator.js and instead load data only after the first filter is applied

Currently, I am utilizing the tabulator.js library and hoping to delay the loading of data until after the first filter is applied, rather than immediately upon page load. Is there a way to achieve this functionality using something like: initialAjaxLoa ...

Encountering an issue: Unable to load resources - Server returned a status code of 500

Struggling with implementing ajax code in my app development. I've encountered an issue where the ajax code that works fine in the video tutorials I'm following doesn't seem to work for me. It's really frustrating! Here is a snippet of ...

Abbreviating Column Labels in Google Visualization

Is there a way to use the google visualization API to display column headers in an abbreviated form in a table, but show the full labels in a pie chart using the same dataset? Check out this snippet of JavaScript: //create the dashboard and table chart ...

underscore.js is throwing an error with the message "unexpected token ILLEGAL

<% var cache_badge = ''; for (var i=0; i<badges.length; i++) { cache_badge += " <a href='' data-toggle='popover' data-content='test' data-original-title='test'><img style='padding-r ...