Zum Hauptinhalt springen

Vince

Typescript examples where the Typesystem fails

function shouldMutateArray(items: (number | string)[]) {
items.push("woops");
}
const calculationOnNumbers = (items: number[]) => items.reduce((a, b) => a - b, 0)

const items: number[] = [1, 2, 3]; // only numbers allowed in here

console.log(items); // -> [1, 2, 3]
console.log(calculationOnNumbers(items)); // -6

shouldMutateArray(items);
// ! we now have a STRING in our array of NUMBERS...

console.log(items); // -> [1, 2, 3, "woops"]
console.log(calculationOnNumbers(items)); // -> NAN

type ReadonlyStruct = {
readonly a: number;
readonly b: number;
}

type NormalStruct = {
a: number;
b: number;
}

function newReadonly(): ReadonlyStruct {
return {
a: 1,
b: 2,
};
}

function willMutateNormalStruct(item: NormalStruct) {
item.a = 99;
item.b = 88;
}

const item = newReadonly();

console.log(item); // { a: 1, b: 2 }

// item.a = 1; // <- this is not allowed. Not Allowed to mutate readonly field

willMutateNormalStruct(item);

console.log(item); // { a: 99, b: 88 }

// ! typescript can't properly differentiate between the 2 Structs with same name fields
// !!! typescript just ignore the readonly annotation and mutation of those fields becomes possible

Vince
  • Implementiere die mit :todo markierten funktionen
/** Speichert Kundendaten - pro Kunden nur ein Datensatz*/
class Kunde{
// --Attribute--
vorname = "";
nachname = "";
alter = 0;
geschlecht = "";

//Konstruktor
constructor(vor, nach, alt, gesch){
this.vorname = vor;
this.nachname = nach;
this.alter = alt;
this.geschlecht = gesch;
}


// --Methoden--
// bei Hochzeit ect kann sich Nachname ändern
setNachname(neuerName){
this.nachname = neuerName;
}
// Jedes Jahr unvermeidbar
hatteGeburtstag(){
this.alter += 1;
}

// Gibt alle Kundendaten aus
getInfo(){
let isVolljaehrig = false;
if (this.alter > 17){
isVolljaehrig = true;
}
return ("Kunde "+this.vorname
+ " " + this.nachname
+ " ist " + this.alter + " Jahre alt, "
+ this.geschlecht + " und "
+ (isVolljaehrig ? "volljährig" : "minderjährig")
+ ". <br>"
);
}
}

console.log
---press run to see if your code works---

Vince

Write a function called provisionsRechner, that takes in a float and adheres to the Diagramm below.

Struktogramm

console.log
---press run to see if your code works---

Vince

Experimental .js-file -> read code and autogenerate elements and answers.

== und ===

/md/jsAufgaben/ae5-1.js
loading js-file...

    Vergleiche, AND && und OR ||

    /md/jsAufgaben/ae5-2.js
    loading js-file...

      eine kleine Funktion

      /md/jsAufgaben/ae5-3.js
      loading js-file...

        Vince

        Gesucht sind die 6 Zeilen die der ConsoleLog ausgibt. Sollten Lösbar sein wenn man in Js-Hero bis ca 37. gekomen ist.

        function x(a){
        return a % 10;
        }

        function y(str){
        return str.length;
        }

        function pr(a,b){
        console.log(a+b+c);
        }

        // 1 Block | strings and numbers
        let a = "hello world!";
        let b = x(a.length);
        let c = y(a);
        pr( a, b + c );
        pr( c, b + a );

        // 2 Block | charAt() und substr() JS-Hero 22-26
        a = "This_string_has_26_letters";
        b = "This_string_has_29_characters";
        c = b.charAt(23) + b.charAt(4) + b.substr(5,12) + a.charAt(16) + a.substr(19,7);
        console.log(c);
        pr(c.length, "isTheLengthOf:");

        // 3 Block | Math-functions JS-Hero 32-37
        a = y("12345") + Math.random();
        b = Math.ceil(a) + Math.floor(a);
        c = c.length;
        console.log(b);
        pr(b,b);

          Vince

          Gesucht sind die 8 Zeilen die der ConsoleLog ausgibt.

          var x = "Hello";
          var y = 2;
          var z = 3;
          printOut();

          x = a();
          y = a() + b(1);
          z = x + a();
          printOut();

          x = c(1, 2);
          y = c(x, 4);
          z = c(z, b(y)) - 2;
          printOut();

          console.log(x + y + z + "");
          console.log("" + x + y + z);

          function a() {
          let a = 1;
          a = a + 1;
          a += 3;
          return a;
          }

          function b(base){
          return base * base;
          }

          function c(a, b){
          return x + a + b;
          }

          function printOut(){
          console.log(x + y + z);
          console.log("Ergebnis:_" + x + y+ z);
          }

            Vince

            Gesucht sind die 2 Zeilen die der ConsoleLog ausgibt.

            let var1 = 3;
            let var2 = addiere(3, 2);
            let var3 = 31;
            var3 = multipliziere(var1, 1);
            let var4 = hoch2(5);
            let var5 = hoch3(5, 10, 15);
            var1 = addiere(var2, 5);
            console.log(var1 + " " + var2 + " " + var3+ " " + var4 + " " + var5);
            console.log(var1 + var2 + " " + var3 + var4 + " " + var5);


            function addiere(x, y){
            let z = x + y + 1;
            return x + y;
            }

            function multipliziere(a, b){
            let x = a;
            return x*a;
            }

            function hoch2(num){
            return num*num;
            }

            function hoch3(num1, num2, num3){
            return num1 * num2 * num1;
            }

              Vince

              Gesucht sind die 4 Zeilen die der ConsoleLog ausgibt.

              let aNumber;
              let anotherNumber;

              function changeNumber(number){
              aNumber = number;
              console.log(number);
              return anotherNumber;
              }

              function whatDoesThisDo(xyz){
              console.log(xyz - 100);
              return aNumber + 1;
              console.log(xyz + aNumber);
              }

              anotherNumber = 22;
              changeNumber(10);
              changeNumber(anotherNumber);
              whatDoesThisDo(100);
              anotherNumber = whatDoesThisDo(aNumber);

                Vince

                console.log
                ---press run to see if your code works---

                Vince

                Default-Info about Docusaurus blog

                Docusaurus blogging features are powered by the blog plugin.

                Simply add Markdown files (or folders) to the blog directory.

                Regular blog authors can be added to authors.yml.

                The blog post date can be extracted from filenames, such as:

                • 2019-05-30-welcome.md
                • 2019-05-30-welcome/index.md

                A blog post folder can be convenient to co-locate blog post images:

                Docusaurus Plushie

                The blog supports tags as well!

                And if you don't want a blog: just delete this directory, and use blog: false in your Docusaurus config.