Exercicis d'ampliació

per Roger Borrell darrera modificació 2020-03-25T16:18:08+02:00
Exercicis d'ampliació per acabar de afiançar els nous conceptes

  • Fes que el nom i el cognom estiguin en variables diferents

<html>
<body>
<script type="text/javascript">
var nom = "Jaume"
var cognom = "Borràs"
var nota = 4.9
document.write("<h4>NOM: " + nom + " " + cognom + "</h4><hr/>")
document.write("<h5>NOTA: " + nota + "</h5>")

</script>
</body>
</html>
  • Afegeix les variables curs,grup i professor
<html>
<body>
<script type="text/javascript">
var nom = "Jaume"
var cognom = "Borràs"

var nota = 4.9
var curs = "ESI 2"
var grup = "A"
var professor = "Roger Borrell"

document.write("<h4>NOM: " + nom + " " + cognom + "</h4><hr/>")
document.write("<h5>NOTA: " + nota + "</h5>")
document.write("<h5>CURS: " + curs + grup + "</h5>")
document.write("<h5>PROFESSOR: " + professor + "</h5>")

</script>
</body>
</html>

7

  • Millora el format de sortida
<html>
<head>
<body>
<script type="text/javascript">
var nom = "Jaume Borràs"
var nota = 4.9
var aprovat
if (nota >=5)
{
aprovat=true
}
else
{
aprovat=false
}
document.write("<p>NOM: " + nom + "</p>")
document.write("<p>NOTA: " + nota + "</p>")
document.write("<p>APROVAT: " + aprovat + "</p>")
</script>
</body>
</html>

  • Fes que enlloc de true o false la sortida sigui Aprovat o Suspès (Cal mantenir la variable booleana Aprovat)
<html>
<head>
<body>
<script type="text/javascript">
var nom = "Jaume Borràs"
var nota = 4.9
var aprovat
if (nota >=5)
{
aprovat=true
}
else
{
aprovat=false
}

document.write("<p>NOM: " + nom + "</p>")
document.write("<p>NOTA: " + nota + "</p>")

if (aprovat==true)
{
document.write("<p>APROVAT: Aprovat</p>")
}
else if (aprovat==false)
{
document.write("<p>APROVAT: Suspès</p>")
}
</script>
</body>
</html>

9

  • Fes un programa que demani el dia de la setmana i digui si es festiu o laborable

a) Mitjançant l'ús de la estructura if

<html>
<head>
<script type="text/javascript">
function mostra_dialeg()
{
var dia=prompt("Digue'm un dia de la setmana","Dilluns")
if (dia!=null && dia!="")
{
var diaMaj=dia.toUpperCase() //Canvio la variable "dia" a majscules.

if (diaMaj=="DILLUNS" || diaMaj=="DIMARTS" || diaMaj=="DIMECRES" || diaMaj=="DIJOUS" || diaMaj=="DIVENDRES" )
{
document.write("El "+ dia + " és un laborable")
}
else if (diaMaj=="DISSABTE" || diaMaj=="DIUMENGE")
{
document.write("El "+ dia + " és un festiu")
}
else
{
document.write(dia + " no és cap dia de la setmana")
}
}
}
</script>
</head>
<body>
<input type="button" onclick="mostra_dialeg()" value="Dia" />
</body>
</html>

b) Mitjançant l'ús de la estructura switch

<html>
<head>
<script type="text/javascript">
function mostra_dialeg()
{
var dia=prompt("Digue'm un dia de la setmana","Dilluns")
if (dia!=null && dia!="")
{
switch (dia.toUpperCase()) //Canvio la variable "dia" a majscules.
{
case "DILLUNS":case "DIMARTS":case "DIMECRES":case "DIJOUS":case "DIVENDRES":
document.write("El "+ dia + " és un laborable")
break
case "DISSABTE":case "DIUMENGE":
document.write("El "+ dia + " és un festiu")
break
default:
document.write(dia + " no és cap dia de la setmana")
}
}
}
</script>
</head>
<body>
<input type="button" onclick="mostra_dialeg()" value="Dia" />
</body>
</html>