Learn the process of creating a calculator using JavaScript in ASP.NET

Hey there! I'm looking to create a calculator in asp.net. I'd like to use Java Script alongside asp without the need for html and ajax. Any suggestions or guides on how to do this?

Answer №1

If you're looking to dive into JavaScript, a great starting point can be found here. While creating an ASP project for a simple calculator could be an option, it's not necessary and might be more than what you need.

For those interested in jQuery, there is a fantastic tutorial for a jQuery calculator that can be accessed here.

Answer №2

If you're looking to create a simple calculator in a web browser, having knowledge of HTML is essential. Without it, you won't be able to have any input controls for your users. Fortunately, making a basic calculator with just a few lines of JavaScript - no jQuery required - is quite manageable. Here's an example:

<script type="text/javascript">
    function calculate()
    {
        var v = document.getElementById("data");
        alert(eval(v.value));
    }
</script>


<input type="text" id="data" />
<input type="button" value="Calculate" onclick="calculate()" />

Simply enter your equation into the text box (e.g., 2 + 4 / 10) and click the button to get the result!

Answer №3

var counter = 0;

function addCounter(value) {
    var inputValue = document.getElementById("text1").value;
    var length = inputValue.length;
    var lastChar = inputValue.charAt(length - 1);
    
    if (lastChar != value) {
        counter = 0;
    }
    
    counter = parseInt(counter) + parseInt(1);
    
    if (counter <= 1) {
        document.getElementById("text1").value += value;
    } else {
        document.getElementById("text1").innerHTML = '';
    }
}

function event1(input) {
    document.getElementById("text1").value += input;
    var evalValue = document.getElementById("text1").value;
    document.getElementById("text3").value = eval(evalValue);
    document.getElementById("text1").value = eval(evalValue);
}

function event2() {
    var x = document.getElementById("text1").value;
    document.getElementById("text1").value = eval(x);
}

function event3() {
    document.getElementById("text1").value = "";
    document.getElementById("text3").value = "";
}

function event4() {
    var inputValue = document.getElementById("text1").value;
    var newInputValue = inputValue.slice(0, -1);
    document.getElementById("text1").value = newInputValue;
}
.curve{
    border-radius: 13px;
}
.col{
    background-color: #00acc1;
}
.col1{
    background-color: #e8f5e9;
}
.col2{
    background-color: #1C2331;
}

#text1{

}
#text3{

}
<html>
<head>
    <title>Custom Calculator</title>
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

</head>
<body>
<div class="curve" style="text-align: center;">
    <table class="curve col2">
        <tr>
            <th colspan=6 class="curve col"><h1>Custom Calculator</h1></th>
        </tr>
        <tr>
            <td colspan=6><input type="text" id="text1" class="curve col1" style="text-align:right;width:100%;"/></td>
        </tr>
        <tr>
            <td colspan=6><input type="text" id="text3" class="curve col1" style="text-align:right;width:100%;"/></td>
        </tr>
        <tr>
            <td><button type="button" value="/" onClick="addCounter(this.value)" class="curve btn-primary" style="padding:8px 14px;">/</button></td>
            <td><button type="button" value="." onClick="addCounter(this.value)" class="curve btn-primary" style="padding:8px 14px;">.</button></td>
            <td><button type="button" value="*" onClick="addCounter(this.value)" class="curve btn-primary" style="padding:8px 14px;">*</button></td>
            <td><button type="button" value="-" onClick="addCounter(this.value)" class="curve btn-primary" style="padding:8px 14px;">-</button></td>
            <td><button type="button" value="" onClick="event3(this.value)" class="curve btn-primary" style="padding:6px 18px;">C</button></td>
        </tr>
        <tr>
            <td><button type="button" value="7" onClick="event1(this.value)" class="curve btn-primary" style="padding:7px 12px;">7</button></td>
            <td><button type="button" value="8" onClick="event1(this.value)" class="curve btn-primary" style="padding:7px 12px;">8</button></td>
            <td><button type="button" value="9" onClick="event1(9)" class="curve btn-primary" style="padding:7px 12px;">9</button></td>
            <td rowspan=2><button type="button" id="ad" value="+" class="curve btn-primary" style="height:70px;padding:8px 13px;" onClick="addCounter(this.value)">+</button></td>
            <td><button type="button" value="" onClick="event4(this.value)" class="curve btn-primary" style="padding:6px 10px;">DEL</button></td>
        </tr>
        <tr>
            <td><button type="button" value="4" onClick="event1(4)" class="curve btn-primary" style="padding:7px 12px;">4</button></td>
            <td><button type="button" value="5" onClick="event1(5)" class="curve btn-primary" style="padding:7px 12px;">5</button></td>
            <td><button type="button" value="6" onClick="event1(6)" class="curve btn-primary" style="padding:7px 12px;">6</button></td>
        </tr>
        <tr>
            <td><button type="button" value="1" onclick="event1(1)" class="curve btn-primary" style="padding:7px 12px;">1</button></td>
            <td><button type="button" value="2" onClick="event1(2)" class="curve btn-primary" style="padding:7px 12px;">2</button></td>
            <td><button type="button" value="3" onClick="event1(3)" class="curve btn-primary" style="padding:7px 12px;">3</button></td>
            <td rowspan=2><button type="button" class="curve btn-primary" style="height:70px;padding:8px 13px;" onClick="event2()">=</button></td>
        </tr>
        <tr class="curve">
            <td><button type="button" class="curve btn-primary" onclick="event1(0)" style="padding:7px 12px;">0</button></td>
        </tr>
    </table>
</div>

</body>
</html>

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

Tips for creating equal height columns in Bootstrap 4

Working with Bootstrap 4 Beta in an attempt to create two columns that maintain full height regardless of screen size. The code below functions perfectly, ensuring both columns maintain full height across all devices. However, upon inserting the bootstra ...

The UI does not reflect changes to the scope variable after it is updated by a service call

When I add a new item to the scope from another service, the select tag options that come from the service do not reflect the newly inserted value immediately. The new option only appears after refreshing the page. It seems like the scope is not updating t ...

Obtaining data from a CSV file and transforming it into JSON format results in an array

Currently, I am working on a function that takes a JSON object and prints it out as strings: GetAllArticles: (req, res) => { var allArticles = getAllArticles(); res.setHeader("Content-Type", 'application/json'); res.w ...

Ways to extract values from a JSON output using comparisons

I am dealing with a JSON data structure that contains information about various teams. My task is to compare the values in the teams array with the values stored in html_content.position[i]. In this comparison, the index i needs to be dynamically set withi ...

Any suggestions for rotating a (THREE.mesh) 3D object around another object that is already rotating on an axis?

I am trying to create a simulation where a moon orbits around a planet that is itself orbiting around a sun. Using three.js, I have successfully set up the Earth rotating around the Sun. However, I am facing challenges in making the Moon rotate around the ...

The process of creating a React build varies greatly from the initial development phase

Thank you for offering to help me! After building my React web app, it looks very different from development mode. I use serve -s build to monitor the app build. However, even on my online DigitalOcean server, it doesn't appear the same as in develop ...

struggling to navigate through JSON array obtained from $.ajax

Having an issue with assigning data to a div on my view page. The code snippet causing the problem is as follows: $.ajax({ url: "/api/Flight/SearchFlight", type: "Post", data: $('form').serialize() + '&' + $.param({ &apos ...

The Optimal Approach for Importing Libraries across Multiple Files

I have two files - one containing the main code execution, and the other solely consisting of a class. For instance: File_1: const _ = require('underscore'), CoolClass = require('CoolClass'); _.map(//something) Files_2: const _ = ...

Using jQuery to animate a DIV sliding in from the side

When a user scrolls down (x) pixels, I'm smoothly sliding a DIV into the viewport horizontally. If the user scrolls back up, the DIV scrolls out again. However, the sliding motion appears jerky and pauses at certain moments. <div id="doom_o">&l ...

JavaScript does not recognize external styles (when an if statement is involved)

I recently started learning web development, and I encountered an issue while working on a responsive design project. When the hamburger menu is clicked, the lines for expansion that are defined in an external CSS file are not being detected. Instead, new ...

What is the proper way to handle errors within a try / catch block when working with asynchronous functions?

My code snippet: async function doRequest() { try { const response = await performAsyncTask(); if (response.isError) { throw new Error(response.isError); } return 'Everything is good'; } catch (err) { console.log(er ...

Jquery cannot be used to set a value for an ASP .net hidden field

Having trouble setting the value of a hidden field using jQuery in ASP .NET. The hidden field is defined as follows: <asp:HiddenField runat="server" ID="hdnSelectedTicket" /> This is how I am attempting to set the value: alert(ticketI ...

Sending a message to the parent window of the browser through a callback function in Internet Explorer version 11.0.x

Currently, I am working on developing a callback in my application that is responsible for opening a popup, executing some tasks, and then responding to the popup's opener window to indicate that the action has been completed. In order to communicate ...

Unable to impose a restriction on the number input field limit

My input field has a type of "number" with the min and max attributes applied to limit user input. However, I am facing an issue where users can still enter values beyond the set limit. How can I prevent users from entering values above the specified lim ...

Can you explain the compatibility between comet and PHP?

As I utilize Comet iframe, I simply send script tags from the backend PHP file to the frontend where JavaScript displays them. I would appreciate it if someone could provide a concise explanation of how the comet server fits into the equation and how comm ...

My JavaScript for loop is malfunctioning

Having trouble with the SelectSeat function, it doesn't seem to be working even though I've called it from the HTML onclick event. <html> <head><link rel="stylesheet" type="text/css" href="mandigo.css"> <center> <b&g ...

Extracting information from this array using JavaScript

After struggling for a day, I finally managed to get some output from PHP instead of just the word "array". What a relief! (check out this link for the solution) Basically, this is the data returned from an XMLHTTP request to PHP, handled through a JS ca ...

Concealing div containers and eliminating gaps

Looking for a way to filter div boxes using navigation? Check this out: <ul> <li><a href="javascript:void(0);" data-target="apples">Appels</a></li> <li><a href="javascript:void(0);" data-target="bananas">Ban ...

Executing a function within the constructor in Angular 2/IONIC 2

I am new to Angular 2 and I have a question regarding invoking a child method from the current constructor. Is it possible to call the getPosition method from the constructor? I attempted to do so, but encountered an exception stating "getPosition is not ...

Inconsistencies in date formatting between Javascript and PHP yield varying results

While working with PHP, I noticed this: echo date("Y/m/d",786668400); The output is 1994/12/06 However, when I tried the same thing in JavaScript: console.log(new Date(786668400*1000).getDate() + "." + (new Date(786668400*1000).getMonth() + ...