Calculator with the 4 basic operations and create a place for the user to write their information and display on the alert after the result

-1

Hello, this is my first post on this site and I would like a help, I need to create a calculator so that the user can put the name so that when the alert displays the result, the name appears, for example: '' Hello , Mark, the result of the sum is: X ''

The code is below, I need only the modification to create the box to write the name and the modification in the alert.

Thank you.

<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>
    
asked by anonymous 28.02.2018 / 14:23

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>
    
28.02.2018 / 14:34