Thinking outside the box

Prerequisites

LEVEL: INTERMEDIATE

A good understanding and fluency in JavaScript’s looping mechanisms will be of great help to you on this exercise.

Instructions

Given the code below (HTML+JS), create two more Event Handlers for the multiplication and division of the two numbers.

Rules:

  • You should only work on the JavaScript code.

  • You should not use the * (multiplication) or / (division) JavaScript operators for this task.

  • You have 25 minutes for this exercise.

<input type="number" name="number_a" placeholder="First number">
<input type="number" name="number_b" placeholder="Second number">
<hr>
<button type="button" id="add">Add</button>
<button type="button" id="sub">Subtract</button>
<button type="button" id="mul">Multiply</button>
<button type="button" id="div">Divide</button>
<hr>
<label>Result:</label>
<input type="text" readonly name="result"> 
const numA = document.querySelector("[name='number_a']");
const numB = document.querySelector("[name='number_b']");
const result = document.querySelector("[name='result']");

document.getElementById("add").addEventListener("click", function add(){
  
    result.value = parseInt( numA.value ) + parseInt( numB.value );
  
});

document.getElementById("sub").addEventListener("click", function add(){
  
    result.value = parseInt( numA.value ) - parseInt( numB.value );
  
});

// Write your code here to multiple and divide the two input numbers without using
// the * or / arithmetic operators of JavaScript.

Good luck!

Leave a Reply