home button

Übung 4: JavaScript (JS)

Web-Engineering Wintersemester 2022 - Marian Hönscheid

4.1. Funktionen

Code
                
    function identity( param ){
        return param;
    }
    
    function identity_function( param ){
        return function(param){ 
            return identity(param);
        }
    }
    
    function add(x, y){
        return x + y;
    }
    
    function mul(x, y){
        return x * y;
    }
    
    function addf(x){
        return function(y){
            return add(x, y);
        }
    }
    
    function applyf( func ){
        return function(x){
            return function(y){
                return func(x,y);
            }
        }
    }
                
            

4.2. Objekte

Code
                
    function Auto(kennzeichen, farbe, marke){
        var auto = {
            kennzeichen: kennzeichen,
            farbe: farbe,
            marke: marke,
            besitzer: []
        }
        return auto
    }

    function Person(name, geburtsdatum){
        var person = {
            name: name,
            geburtsdatum: geburtsdatum,
            autos: [],
            addAuto: function(auto){
                if (auto) {
                    person.autos.push(auto);
                    auto.besitzer.push(person);
                }
                return true;
            }
        }
        return person;
    }

    function conflict(auto) {
        return true ? auto.besitzer.length > 1 : false;
    }
                
            

4.3. Fibonacci

1. Die größte Fibonacci-Zahl, die sich noch mit Integer sicher speichern lässt ist 8944394323791464.
Sie befindet sind bei Index 78 also ist es die 79. Fibonacci Zahl.

2. Die größte Fibonacci-Zahl, die sich noch als Number sicher speichern lässt ist 130698922376339931803631155380271983098392443907412640726006659460192793070479231740288681087777017721095463154979012276234322246936939647185366706368489362660844147449941348462800922755818969634743348982916424954062744135969865615407276492410653721774590669544801490837649161732095972658064630033793347171632.
Sie befindet sind bei Index 1476 also ist es die 1477. Fibonacci Zahl.

Code
                
    var fibonacci = [0n,1n]
    for (var i=0; i < 1998; i++){
        var next = fibonacci[fibonacci.length-1] + fibonacci[fibonacci.length-2];
        fibonacci.push(next);
    }
    for (i in fibonacci){
        console.log(fibonacci[i])
    }
    function biggestInt(){
        var biggestInt;
        var biggestIntIndex;
        for (i in fibonacci){
            if (fibonacci[i] <= Number.MAX_SAFE_INTEGER){
                biggestInt = fibonacci[i];
                biggestIntIndex = i;
            }
        }
        var biggestIntPosition = BigInt(biggestIntIndex) + 1n;
        console.log ("Die größte Fibonacci-Zahl, die sich noch als Integer sicher speichern lässt ist " + biggestInt + ".");
        console.log ("Die größte Fibonacci-Zahl, die sich noch als Integer sicher speichern lässt befindet sind bei Index " + biggestIntIndex + " also ist es die " + biggestIntPosition + ". Fibonacci Zahl.");
    }
    function biggestNum(){
        var biggestNum;
        var biggestNumIndex;
        for (i in fibonacci){
            if (fibonacci[i] <= Number.MAX_VALUE){
                biggestNum = fibonacci[i];
                biggestNumIndex = i;
            }
        }
        var biggestNumPosition = BigInt(biggestNumIndex) + 1n;
        console.log ("Die größte Fibonacci-Zahl, die sich noch als Number sicher speichern lässt ist " + biggestNum + ".");
        console.log ("Die größte Fibonacci-Zahl, die sich noch als Number sicher speichern lässt befindet sind bei Index " + biggestNumIndex + " also ist es die " + biggestNumPosition + ". Fibonacci Zahl.");
    }
                
            

4.4. TopSort

Code
                
    function topsort(liste){
        var tmp = []
        while (liste.length > 0){
            var paar = liste.pop();
            if (tmp.length === 0){
                tmp = [...paar];
            } else {
                if (tmp.includes(paar[1])){
                    tmp.splice(tmp.indexOf(paar[1]),0,paar[0]);
                } else {
                    tmp.concat(paar);
                }
            }
        }
        return tmp;
    }
                
            
Zurück