Create a full stack program that acts as a simple printing calculator in javascript

I have an exercise to perform Simple Calculation using Javascript. The code works fine in Visual Studio but not in the Hackerrank test site that i have to do the exercise in.

HTML Code: [Given already, cannot modify] :


     Simple Calculation




    
        




JS file that i can modify.I cannot remove the various functions or add a new one

var text = "";
function update[value] {
    //Type the code here.
    text+= value;
    document.getElementById['screen'].value = value;
}

function result[] {
    //Type the code here.
    document.getElementById['screen'].value = eval[text];
}

function form_reset[] {
    //Type the code here.
    document.getElementById['screen'].value ="";
    text = "";
}

When i run the test, its validating against below test cases. It's failing in the function result[]; while adding 7 and 8

describe['Calc Handson', function[] {
    beforeEach[function[] {
    document.body.innerHTML='




'; }]; afterEach[function[] { document.body.removeChild[document.getElementById['app']]; }]; describe['Calc ', function[] { it['update function should exist', function[] { update[1]; expect[document.getElementById["screen"].value].toBe['1']; }]; it['update function should exist', function[] { update[2]; expect[document.getElementById["screen"].value].toBe['2']; }]; it['update function should exist', function[] { update[3]; expect[document.getElementById["screen"].value].toBe['3']; }]; it['update function should exist', function[] { update[4]; expect[document.getElementById["screen"].value].toBe['4']; }]; it['update function should exist', function[] { update[5]; expect[document.getElementById["screen"].value].toBe['5']; }]; it['update function should exist', function[] { update[6]; expect[document.getElementById["screen"].value].toBe['6']; }]; it['update function should exist', function[] { update[7]; expect[document.getElementById["screen"].value].toBe['7']; }]; it['update function should exist', function[] { update[8]; expect[document.getElementById["screen"].value].toBe['8']; }]; it['update function should exist', function[] { update[9]; expect[document.getElementById["screen"].value].toBe['9']; }]; it['update function should exist', function[] { update[0]; expect[document.getElementById["screen"].value].toBe['0']; }]; it['update function should exist', function[] { update['*']; expect[document.getElementById["screen"].value].toBe['*']; }]; it['update function should exist', function[] { update['+']; expect[document.getElementById["screen"].value].toBe['+']; }]; it['update function should exist', function[] { update['-']; expect[document.getElementById["screen"].value].toBe['-']; }]; it['update function should exist', function[] { update['/']; expect[document.getElementById["screen"].value].toBe['/']; }]; it['result function should exist', function[] { update[7]; update['+']; update[8]; result[]; expect[document.getElementById["screen"].value].toBe['15']; }]; it['form_reset function should exist', function[] { update[7]; form_reset[]; expect[document.getElementById["screen"].value].toBe['']; }]; }]; }];

Error that i am getting is :

Node.js [linux; U; rv:v8.9.4] Calc Handson Calc  result function should exist FAILED                                                   
        SyntaxError: Invalid regular expression: missing /                                                                             
            at result [app/index.js:10:52]                                                                                             
            at UserContext. [test/index_test.js:88:6]                                                                       
Node.js [linux; U; rv:v8.9.4]: Executed 16 of 16 [1 FAILED] [0.175 secs / 0.178 secs]

Kindly help.

How do you code a calculator in JavaScript?

const number1 = parseFloat[prompt ['Enter the first number: ']]; const number2 = parseFloat[prompt ['Enter the second number: ']]; let result; // declaration of the variable. // use if, elseif and else keyword to define the calculator condition in JavaScript.

How can we create a simple calculator using HTML CSS and JavaScript?

You can use DOM selectors to target various components of a page..
function clearScreen[] { document.getElementById["result"].value = ""; }.
function display[value] { document.getElementById["result"].value += value; }.
function calculate[] { var p = document.getElementById["result"].value; var q = eval[p];.

How do you make a calculator with node js?

Step 1: Create and Open your project folder in the Code Editor. Step 2: Create a file 'app. js': Inside this file, we are going to write the code. Step 3: Create a constant 'argvs' and set it to process.

Chủ Đề