13 exercícios de Matemática em Javascript

Ver versão no site antigo com uma melhor formatação:

https://sites.google.com/a/aebenfica.org/apontamentos-tic/programacao/javascriptcodigos/matematica


Vamos implementar estes 13 exercícios de Matemática:

Alguns já se encontram no GitHub de exemplo em: github.com/lpitta/Matematica

1. add2.html: Adiciona dois números

<script>

var num1, num2, sum; num1 = prompt("Enter first number"); num2 = prompt("Enter second number"); sum = parseInt(num1) + parseInt(num2) // "+" means "add"; alert("Sum = " + sum) // "+" means combine into a string;

</script>

2. addup.html: Adiciona números consecutivos até digitarmos 0

<script>

var sum = 0; var num; num = prompt("Enter new value, or 0 to end"); while (num != 0) { sum = sum + parseInt(num); num = prompt("Enter new value, or 0 to end"); } alert("Sum = " + sum);

</script>

3. area.html: computes area of circle from radius, until no input

<script>

var rad, area; rad = prompt("Enter radius"); while (rad != null) { area = 3.14 * rad * rad; alert("radius = " + rad + ", area = " + area); document.write("<br> radius = " + rad + ", area = " + area); rad = prompt("Enter radius"); }

</script>

4. areafcn.html: computes area of circle with function

<script>

var rad; do { rad = prompt("Enter radius") if (rad == "" || rad == null) break; alert("radius = " + rad + ", area = " + area(rad)); } while (rad != "" && rad != null); function area(r) { return 3.141592654 * r * r; }

</script>

5. firstname.html: computes first name alphabetically (terminate by "" or 0)

<script>

var first, name; first = name = prompt("Enter new name, or OK to end") while (name != "" && name != null) { if (name < first) first = name name = prompt("Enter new name, or OK to end") } document.write("First name alphabetically = " + first)

</script>

6. max.html: find maximum numeric value until it sees 0

<script>

var max = 0; var num; num = prompt("Enter new value, or 0 to end"); while (num != 0) { if (parseFloat(num) > max) max = num; num = prompt("Enter new value, or 0 to end"); } document.write("<P> Max = " + max);

</script>

7. name2.html: concatenate two names

<script>

var firstname, secondname, result; firstname = prompt("Enter first name"); secondname = prompt("Enter last name"); result = firstname + secondname; // + means "join" here alert("hello, " + result);

</script>

8. ring.html: computes area of ring with function

<script>

var r1, r2; r1 = prompt("Enter radius 1"); while (r1 != null) { r2 = prompt("Enter radius 2"); alert("area = " + (area(r1) - area(r2))); // parens are needed! r1 = prompt("Enter radius 1"); } function area(r) { return 3.14 * r * r; }

</script>

9. sign.html: computes sign of number

<script>

var num = prompt("Enter number"); while (num != null) { num = parseInt(num); if (num > 0) { alert(num + " is positive"); } else if (num < 0) { alert(num + " is negative"); } else { alert(num + " is zero"); } num = prompt("Enter number"); }

</script>

10. sort.html: sort a set of names

<script>

var name, i = 0, j, temp;

var names = new Array();

// fill the array with names

name = prompt("Enter new name, or OK to end");

while (name != "") {

document.write("<br> " + name);

names[names.length] = name;

name = prompt("Enter new name, or OK to end");

}

document.write("<P> " + names.length + " names");

// insertion sort

for (i = 0; i < names.length-1; i++) {

for (j = i+1; j < names.length; j++) {

if (names[i] > names[j]) {

temp = names[i];

names[i] = names[j];

names[j] = temp;

}

}

}

// print names

for (i = 0; i < names.length; i++) {

document.write("<br> " + names[i]);

}

</script>

11. while.html: computes and prints i, i**2 in a while loop, up to n

<script>

var n = prompt("Enter number"); while (n != null) { i = 0; while (i <= n) { document.write("<br>" + i + " " + i*i); i = i + 1; } n = prompt("Enter number"); }

</script>

12. loop.html: infinite loop: careful! If you run this you will have to "force quit" your browser!

Perigoso! Temos de forçar o fecho do navegador!

O código não foi copiado para aqui pois este ciclo sendo infinito provoca o bloqueio do navegador:


13. for.html: Foxtrot for loop... (Repete a mesma frase 500 vezes)

<script>

alert("ready?"); for (count = 1; count <= 500; count++) document.write("<br>" + count + " I will not throw paper airplanes in class.");

</script>