The conditions specified in my JavaScript if statement are seemingly being disregarded, even after I forcefully alter the variable at the conclusion of the function

var app = angular.module("calculatorApp", []);

app.controller("CalculatorCtrl", CalculatorCtrl);

function CalculatorCtrl() {
    pleaseWork = null;
    this.buttonClicked = function(button) {
        if (pleaseWork = null)
        {
            this.secondNumber = button;
            pleaseWork = "I don't know why this won't work."
        } else {
            this.firstNumber = button;
        }
    }
    this.opButtonClicked = function(opButton) {
        this.operationType = opButton;
    }
    this.equalButtonClicked = function() {
        var firstNumber= parseFloat(this.firstNumber);
        var secondNumber= parseFloat(this.secondNumber);
        if (operationType === '+') {
            this.equalButtonClicked = firstNumber + secondNumber;
        }
        if (operationType === '-') {
            this.equalButtonClicked = firstNumber - secondNumber;
        }
        if (operationType === '*') {
            this.equalButtonClicked = firstNumber * secondNumber;
        }
        if (operationType === '/') {
            this.equalButtonClicked = firstNumber / secondNumber;
        }
    }
}
<html ng-app="calculatorApp">
    <head>
        <title>Calculator</title>
        <script src="angular.js"></script>
        <script src="app.js"></script>    
    </head>

    <body>
        <h1>Midterm Calculator</h1>
        <div ng-controller = "CalculatorCtrl as ctrl">
            <p>
                <span ng-bind="ctrl.firstNumber"></span>
                <span ng-bind="ctrl.operationType"></span>
                <span ng-bind="ctrl.secondNumber"></span>
            </p>
            <p>
                <input type="text" value="{{solution}}" readonly/>
            </p>
            <p>
                <button ng-click="ctrl.buttonClicked('1')">1</button>
                <button ng-click="ctrl.buttonClicked('2')">2</button>
                <button ng-click="ctrl.buttonClicked('3')">3</button>
            </p>
            <p>
                <button ng-click="ctrl.buttonClicked('4')">4</button>
                <button ng-click="ctrl.buttonClicked('5')">5</button>
                <button ng-click="ctrl.buttonClicked('6')">6</button>
            </p>
            <p>
                <button ng-click="ctrl.buttonClicked('7')">7</button>
                <button ng-click="ctrl.buttonClicked('8')">8</button>
                <button ng-click="ctrl.buttonClicked('9')">9</button>
            </p>
            <p>
                <button ng-click="ctrl.buttonClicked('0')" >0</button>
                <button ng-click="ctrl.equalButtonClicked()">=</button>
                <button ng-click="ctrl.opButtonClicked('+')">+</button>
            </p>
            <p>
                <button ng-click="ctrl.opButtonClicked('-')">-</button>
                <button ng-click="ctrl.opButtonClicked('*')">*</button>
                <button ng-click="ctrl.opButtonClicked('/')">/</button>
            </p>

        </div>
    </body>
</html>

The issue lies in the JavaScript if statement between the first and second numbers. Despite setting a variable ("pleaseWork") to null, it still fails to be read correctly. This has caused some frustration as I have not encountered this problem with Angular or JS before. Researching solutions mostly brings up ngifelse statements that I am hesitant to try at this point.

Answer №1

Revise the if statement to include a comparison rather than an assignment

if (pleaseWork == null)

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

Looping through JSON keys using ng-repeat in AngularJS

I am currently working on a project where I need to populate some JSON data retrieved from the Google Tag Manager API and then send this information to the frontend which is developed in AngularJS. At the moment, I am utilizing ng-repeat on a card compone ...

Set up a local storage system to store the background color for my page

I have a unique feature on my website where clicking a button generates a random background color each time. However, I am looking to enhance this by implementing a localstorage function that will remember the last clicked color even after closing or reloa ...

transform the outcome of a $lookup operation into an object rather than an array

When performing a $lookup from a _id, the result is always 1 document. This means that I would like the result to be an object instead of an array with one item. let query = mongoose.model('Discipline').aggregate([ { $match: { ...

Having difficulty retrieving the specific Laravel validation error message through AJAX

In my Laravel project, the error message I am encountering is: {message: "The given data was invalid.", errors: {oData: ["validation.required"]}} errors: {oData: ["validation.required"]} message: "The given data was invalid." I am not trying to display ...

The modal template in Angular UI is not displaying properly with Bootstrap styling

I've been working on displaying a modal template when a row is selected on a table. The issue I'm facing is that upon clicking a row, a 2px thick black shadowed line appears, which seems to represent the modal but doesn't display its conten ...

Transfer data from distinct arrays to separate variables

I have 2 arrays structured like this: let dataA = [{"id": "a1", "name": "Alpha"}, {"id": "a2", "name": "Beta"}, {"id": "a3", "name": "Gamma&quo ...

Leveraging the content delivery network for react-select in a react.js project

I'm having trouble using react-select with cdn. I attempted to download the cdn for react-select but keep encountering an error stating that 'select is not defined'. I also tried downloading the zip package for react-select, but I am unsure ...

The upcoming development does not involve creating an entire HTML webpage using on-demand static site generation (SS

I’m encountering a problem when utilizing getStaticPaths and getStaticProps to create an on-demand SSG for a sharing page. My setup involves Next v12.1.0 and React 17.0.2. After building a specific /[id] page, I can retrieve the data but the HTML output ...

Trouble with React Native Maps: Region not refreshing, causing map to remain stuck on a single location

My <MapView> is not updating the region properly when I scroll the map. It seems to be stuck on one region and doesn't update as expected. Even when I scroll, the same region is passed to this.props.onRegionChange, preventing the map from actual ...

What could be the reason for the data being retrieved but not showing up on the web page?

When fetching data from an API, I encounter a problem where the Loader displays but the data never shows up on the page. The inconsistency of this behavior is puzzling to me. This issue seems to be more prevalent on smartphones than on computers. useEffec ...

Load HTML table values dynamically with Ajax post page load in PHP

My goal is to retrieve the connectivity status of available servers in a database on a PHP page. <tbody> <?php foreach ($data['servers'] as $server) { ?> <tr> <td class=""><?php echo $server->server_ ...

Is there a way to customize the Webpack output to incorporate specific options solely for a particular bundle?

Currently, I am using Webpack 4 to build multiple bundles. My requirement is to add the output options libraryTarget and library for a single bundle only. The default configuration looks like this: output: { path: path.resolve(__dirname, 'dist/j ...

Regardless of the circumstances, the function will always be triggered when $("123") is ready

Encountering an issue where the $().ready function is triggering before the page is actually ready no matter what parameter is passed. The project involves a combination of ASP.NET, EXT.NET, and jQuery to enhance functionality. Despite specifying "123" as ...

Ways to retrieve the ID attribute value of the third ancestor above

How can I retrieve the ID attribute of the 3rd parent element when a link is clicked? Take for example: <div id="First Div"> <div class="class A"> <div class="class B"></div> <div class="class C"></div> &l ...

The discovery of a commitment in the statement. The automation of unwrapping promises within Angular statements has been phased out

Struggling with errors while setting up a new AngularJS project. Here is the code for my app and controller; var app = angular.module('myApp', ['localytics.directives']) .config(['$parseProvider', function ($parseProvide ...

What is the best way to adjust the size of an image within a hyperlink using jQuery?

I'm trying to grab an image from a hyperlink and resize it for display to the user. I've written the code below, but it's not working as expected. Can anyone lend a hand? if ($(this).find("img").attr("src") != null) { va ...

Add the current date plus 48 hours to the content on a webpage (JavaScript maybe?)

Hello there, I am currently in the process of setting up a small online store for a farm using Squarespace. One thing I would like to implement is specifying that items will be available for pickup two days after they are purchased online, instead of imme ...

What is the best way to transfer data from one function to another file in React without directly calling the component or using props?

filename - Header.jsx import { useEffect, useState } from 'react' import { getValue } from './Input' import TextField from '@mui/material/TextField'; export const Header = () => { const [search, setSearch] = useState( ...

Use an array to store nested JSON fields

I'm currently seeking to enhance my proficiency in utilizing JavasScript, React, and Material-UI. I am faced with the challenge of sorting my table using a nested JSON structure and I am encountering difficulties with assigning the JSON fields to my a ...

What is the best way to implement forwardRef in a distinct select component?

Currently, I am utilizing react, typescript, and styled-components to work on my project. Specifically, I am aiming to create a select component for use in React Hook Form. Initially, everything seemed to be in order, but I encountered an error from typesc ...