Condicionals i bucles

If

<!DOCTYPE html>
<html>
<body>
  <p>Click the button to get a time-based greeting:</p>
  <button onclick="myFunction()">Try it</button>
  <p id="demo"></p>

<script>
function myFunction() {
  var greeting;
  var time = new Date().getHours();
  if (time < 10) {
    greeting = "Good morning";
  } else if (time < 20) {
    greeting = "Good day";
  } else {
    greeting = "Good evening";
  }
  document.getElementById("demo").innerHTML = greeting;
}
</script>
</body>
</html>

Exemple

Més informació a: w3schools.com - If

Switch

<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript Switch</h2>

  <p id="demo"></p>

<script>
var text;
switch (new Date().getDay()) {
    case 6:
        text = "Today is Saturday";
        break;
    case 0:
        text = "Today is Sunday";
        break;
    default:
        text = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Exemple

Més informació a: w3schools.com - Switch

For

<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript For Loop</h2>

  <p id="demo"></p>

<script>
var text = "";
var i;
for (i = 0; i < 5; i++) {
    text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Exemple

Més informació a: w3schools.com - For Loop

While

<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript While Loop</h2>

  <p id="demo"></p>

<script>
var text = "";
var i = 0;
while (i < 10) {
  text += "<br>The number is " + i;
  i++;
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Exemple

Més informació a: w3schools.com - While Loop

#FpInfor #Javascript #DAW

Autor: Sergi Coll

results matching ""

    No results matching ""