Calculator with 4 basic operations and create a location for the user to write their information and display in Alert after the result

Asked

Viewed 2,151 times

0

Hello, it’s my first post on this site and I would like a help, I need to create a calculator that the user can put the name so that when Alert displayed the result, the name appeared, for example: ''Hello, Marcos, the result of the sum is: X''

the code is below, I just need the modification to create the box to write the name and the modification in Alert.

from now on thanks.

<html>
<header>
<title>Calculadora</title>
   <script type="text/javascript">
   function somarValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) + parseInt(n2);
   alert(n3);
    }
function multiplicarValores(){
  var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) * parseInt(n2);
   alert(n3);
}   
function dividirValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) / parseInt(n2);
   alert(n3);
}   
function subtrairValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) - parseInt(n2);
   alert(n3);
}   
</script>
</header> 
 <body>
   Hello world
    <legend>somas</legend>
<label>Valor 1:</label>
<input id="s1" type="text"/>
<label>Valor 2:</label>
<input id="s2" type="text"/>
<button id="somar" onclick="somarValores()">Somar</button>
<button id="multiplicar" onclick="multiplicarValores()">multiplicar</button>
<button id="dividir" onclick="dividirValores()">dividir</button>
<button id="subtrair" onclick="subtrairValores()">subtrair</button>
   </body>
   </body>
</html>

1 answer

0


just put another input and concatenate the values:

<html>
<header>
<title>Calculadora</title>
   <script type="text/javascript">
   function somarValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) + parseInt(n2);
   var name = document.getElementById("name").value;
   alert("Olá " + name + ', o resultado é: ' +n3);
    }
function multiplicarValores(){
  var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) * parseInt(n2);
   var name = document.getElementById("name").value;
   alert("Olá " + name + ', o resultado é: ' +n3);
}   
function dividirValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) / parseInt(n2);
   var name = document.getElementById("name").value;
   alert("Olá " + name + ', o resultado é: ' +n3);
}   
function subtrairValores(){
   var n1 = document.getElementById("s1").value;
   var n2 = document.getElementById("s2").value;
   var n3 = parseInt(n1) - parseInt(n2);
   var name = document.getElementById("name").value;
   alert("Olá " + name + ', o resultado é: ' +n3);
}   
</script>
</header> 
 <body>
   Hello world
    <legend>somas</legend>
    <label>Nome:</label>
<input id="name" type="text"/>
<label>Valor 1:</label>
<input id="s1" type="text"/>
<label>Valor 2:</label>
<input id="s2" type="text"/>
<button id="somar" onclick="somarValores()">Somar</button>
<button id="multiplicar" onclick="multiplicarValores()">multiplicar</button>
<button id="dividir" onclick="dividirValores()">dividir</button>
<button id="subtrair" onclick="subtrairValores()">subtrair</button>
   </body>
   </body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.