Manufacturing a Caclulator (HTML / CSS / JS)

AndrewNeedsHelp

I am trying to create a calculator. At the current stage, I am trying to get the screen to display the number that has been clicked. But for some reason it won't display the relevant buttons value. Please can you help me identify the incorrect aspect:

const numberButtons = document.querySelectorAll('[data-number]');
const operationButtons = document.querySelectorAll('[data-operation]');
const equalsButton = document.querySelector('[data-equals]');
const deleteButton = document.querySelector('[data-delete]');
const allClearButton = document.querySelector('[data-all-clear]');
const previousOperandTextElement = document.querySelector('[data-previous-operand]');
const currentOperandTextElement = document.querySelector('[data-current-operand]');


function ready(){
 
    for(let i = 0; i < numberButtons.length; i++){
        var button = numberButtons[i];
        var digitToAdd = button.textContent;
        button.addEventListener('click', addDigitToScreen(digitToAdd));
    }
}

function addDigitToScreen(a){
    let inputValue = document.getElementsByClassName('input')[0].textContent;
    inputValue = inputValue + ' ' + digitToAdd;
}
 <div class="fullCalc">
                <span class="screen">
                <div data-previous-operand class="calculation"></div>
                <div data-current-operand class="input"></div>
                </span>

                <button data-all-clear class="ac-key key">AC</button>
                <button data-delete class="del-key key">DEL</button>
                <button data-operation class="divide-key key">/</button>
                <button data-number class="key-1 num key">1</button>
                <button data-number class="key-2 num key">2</button>
                <button data-number class="key-3 num key">3</button>
                <button data-operation class="multiply-key num key">x</button>
               
                <button data-number class="key-4 num key">4</button>
                <button data-number class="key-5 num key">5</button>
                <button data-number class="key-6 num key">6</button>
                <button data-operation class="plus-key num key">+</button>
                
                <button data-number class="key-7 num key">7</button>
                <button data-number class="key-8 num key">8</button>
                <button data-number class="key-9 num key">9</button>
                <button data-operation class="minus-key adjustor key">-</button>

                <button data-operation class="period-key num key">.</button>
                <button data-number class="key-0 num key">0</button>
                <button data-equals class="equals-key key">=</button>

            
        </div>

The ready function is fired automatically when the page is loaded.

Many thanks!

tommcandrew

There are a few issues.

  1. When you assign the value of the input to a variable (inputValue), you're just making a copy of the value, so when you change it, you're just changing your copy, not the original. To change the original, do it like this:

    let input = document.getElementsByClassName("input")[0]; input.textContent = " " + digitToAdd;

  2. In your addDigitToScreen function, you've named the parameter 'a' but later you're referencing 'digitToAdd' - they need to have the same name.

  3. When the ready() function runs at the start, with every iteration of the loop, you're overwriting the value of 'digitToAdd' because of closure and so every button will end up outputting the value of the last button (0). To avoid this problem, use 'let' instead of 'var'.

  4. When you pass a function to addEventListener, you shouldn't include the parentheses on the end because your function will be automatically called on load. If you want to pass in a function with an argument, like in your example, you can instead use an arrow function which returns the function you want to call with the argument. This way it won't be called on load:

button.addEventListener("click", () => addDigitToScreen(digitToAdd));

  1. Finally, you need to call the ready() function at the start.

I think that's everything. I hope this helps.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

制造Caclulator(HTML / CSS / JS)

来自分类Dev

防止HTML形式的JS / CSS

来自分类Dev

编辑html + css + js下拉导航栏

来自分类Dev

HTML / CSS / JS中的几何图案

来自分类Dev

JS HTML CSS手风琴

来自分类Dev

使用HTML CSS和JS拖动对象

来自分类Dev

保存Darkmode状态html / css / js

来自分类Dev

使用JS抓取HTML文本选择的CSS

来自分类Dev

用于构建UI的JS / HTML / CSS框架

来自分类Dev

在文字上方显示元素-html,css,js

来自分类Dev

HTML / CSS / JS / Bootstrap-对齐内容

来自分类Dev

汇入HTML / Css / JS到CMS?

来自分类Dev

HTML / CSS / JS:不显示简单的div

来自分类Dev

JS在HTML中添加内联CSS

来自分类Dev

自动为文字上色| JS CSS HTML

来自分类Dev

HTML / CSS / JS中未捕获的TypeError

来自分类Dev

CefSharp访问加密的HTML / JS / CSS文件

来自分类Dev

使用Nginx服务html / css / js文件

来自分类Dev

HTML / CSS / JS图像未在线加载

来自分类Dev

PhpStorm - 创建普通的 HTML、JS、CSS 项目

来自分类Dev

HTML、CSS、JS 意外结束输入错误

来自分类Dev

HTML、CSS、JS if 语句认为 1==0

来自分类Dev

document.getElementById JS HTML CSS

来自分类Dev

JS加载应该加载HTML资产(CSS和JS)的外部HTML

来自分类Dev

用替代内容(HTML + CSS + JS)替换整个网页

来自分类Dev

在CSS / HTML / JS中制作某种透明的玻璃图像

来自分类Dev

返回没有CSS / JS干扰的AJAX HTML响应

来自分类Dev

Angular JS指令-CSS文件相对于HTML的位置

来自分类Dev

使用grunt更新html文件中的css / js文件路径