JavaScript – die besten Beiträge

JS Taschenrechner, Dezimal in Binär und andersherum auch?

Hallo Zusammen,
ich bin seit knapp 3 Wochen in meiner Ausbildung und sollte einen Taschenrechner programmieren, was bis jetzt auch geklappt hat. Nun habe ich eine neue Aufgabe dazu bekommen, nämlich mit 2 Radio Buttons mir jeweils anzeigen zu lassen: Binär und Dezimal.
Ich bin absolut hilflos und freue mich über mögliche Lösungen und HIlfe. Danke im voraus.

<!DOCTYPE html>
<html lang="de">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Taschenrechner No. 3</title>
      <script>
         let aktuellesErgebnis = 0;
         let modus;
         let aktuelleZahl;
         function AddLetter(zahl)
         {
         if(modus == null) 
         {
         document.getElementById("textboxDisplay").value = zahl;
         modus = "Nummern";
         }
         else
         {
         document.getElementById("textboxDisplay").value += zahl;
         }
         }
         function Plus()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = "";
         modus = "Plus";
         }
         function Minus()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = "";
         modus = "Minus";
         }
         function Mal()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = "";
         modus = "Mal"
         }
         function Geteilt()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = "";
         modus = "Geteilt"
         }
         function Restwert()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = "";
         modus = "Modulo"
         }
         function Gleich()
         {
         Hilfe();
         document.getElementById("textboxDisplay").value = aktuellesErgebnis;
         modus = null;
         }
         function Hilfe()
         {
         aktuelleZahl = Number(document.getElementById("textboxDisplay").value);
         if(modus == "Plus")
         {
         aktuellesErgebnis += aktuelleZahl;
         }
         if(modus == "Minus")
         {
         aktuellesErgebnis -= aktuelleZahl;
         }
         if(modus == "Mal")
         {
         aktuellesErgebnis *= aktuelleZahl;
         }
         if(modus == "Geteilt")
         {
         aktuellesErgebnis /= aktuelleZahl;
         }
         if(modus == "Modulo")
         {
         aktuellesErgebnis %= aktuelleZahl;
         }
         if(modus == null || modus == "Nummern")
         {
         aktuellesErgebnis = aktuelleZahl;
         }
         }
         function Weg()
         {
         document.getElementById("textboxDisplay").value = "";
         aktuellesErgebnis = 0;
         modus = null;
         }
      </script>
   </head>
   <body>
      <input type="radio" name="Umrechnung" value="Binär" />
      <input type="radio" name="Umrechnung" value="Dezimal" />
      <input id="textboxDisplay" readonly />
      <input type="button" value="1" onclick="AddLetter('1')" />
      <input type="button" value="2" onclick="AddLetter('2')" />
      <input type="button" value="3" onclick="AddLetter('3')" />
      <input type="button" value="4" onclick="AddLetter('4')" />
      <input type="button" value="5" onclick="AddLetter('5')" />
      <input type="button" value="6" onclick="AddLetter('6')" />
      <input type="button" value="7" onclick="AddLetter('7')" />
      <input type="button" value="8" onclick="AddLetter('8')" />
      <input type="button" value="9" onclick="AddLetter('9')" />
      <input type="button" value="0" onclick="AddLetter('0')" />
      <input type="button" value="+" onclick="Plus()" />
      <input type="button" value="-" onclick="Minus()" />
      <input type="button" value="*" onclick="Mal()" />
      <input type="button" value="/" onclick="Geteilt()" />
      <input type="button" value="Mod" onclick="Restwert()" />
      <input type="button" value="Löschen" onclick="Weg()" />
      <input type="button" value="=" onclick="Gleich()" />
   </body>
</html>
HTML, Webseite, JavaScript, Code, Programmiersprache

WebGL - Clip Space | Pixel Space?

// Renderer Beispiel
 render(){
       const s2  = new Square(300, 100, 70, [0, 1, 1, 1]);
       s2.setup(this.context, this.program); 
       s2.render(this.program, this.context, deltaTime);

       this.vertexManager.draw(3, 0, 4, 100, 100, 0, [0,0,1,1]);
 }
// 

export class Square extends Renderable {
    constructor (x, y, size, color) {
        super();
        this.x = x;
        this.y = y;
        this.size = size;
        this.color = color;
        this.vertices = new Float32Array([
            -size / 2, -size / 2,
            size / 2, -size / 2,
            size / 2, size / 2,
            -size / 2, size / 2,
        ]);
        this.vertexBuffer = null;
        this.colorBuffer = null;
        this.transformationMatrix = createIdentityMatrix3();
    }

    setup (gl, program) {
        this.vertexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, this.vertices,  gl.STATIC_DRAW);
    }

    updateTransformationMatrix (x, y, scaleX, scaleY, angle) {
        const translationMatrix = createTranslationMatrix(x, y);
        const rotationMatrix = createRotationMatrix(angle);
        const scalingMatrix = createScalingMatrix(scaleX, scaleY);

        // Combine the matrices: translation * rotation * scaling
        const combinedMatrix = multiplyMatrices(rotationMatrix,          multiplyMatrices(translationMatrix, scalingMatrix));

        // Save the transformation matrix
        this.transformationMatrix = combinedMatrix;
    }


    render (program, gl, camera, deltaTime) {
        gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);

        // Re-enable the vertex attribute array
        const positionLocation = gl.getAttribLocation(program, 'a_position');
        gl.enableVertexAttribArray(positionLocation);
        gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);

        const colorLocation = gl.getUniformLocation(program, 'u_color');
        gl.uniform4fv(colorLocation, this.color);

        const matrixLocation = gl.getUniformLocation(program, 'u_matrix');
        this.updateTransformationMatrix(this.x, this.y, 1.0, 1.0,  0);
        gl.uniformMatrix3fv(matrixLocation, false, this.transformationMatrix);

        gl.drawArrays(gl.TRIANGLE_FAN, 0, this.vertices.length / 2 + 1);
    }
}

export default class VertexManager {
    constructor (gl, program) {
        this.gl = gl;
        this.program = program;
        this.buffers = new Map();
        this.initBuffers();
    }

    initBuffers () {
        this.createBuffer(BUFFER_TYPES.CIRCLE, createPolygonVertices(64, 1));
        this.createBuffer(BUFFER_TYPES.SQUARE, new Float32Array([
            -0.5, -0.5,
            0.5, -0.5,
            0.5, 0.5,
            -0.5, 0.5,
        ]));
    ....
    }

    createBuffer (type, vertices) {
        const buffer = this.gl.createBuffer();
        this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
        this.gl.bufferData(this.gl.ARRAY_BUFFER, vertices, this.gl.STATIC_DRAW);
        this.buffers.set(type, { buffer, vertexCount: vertices.length / 2 });
    }

    draw (type, x, y, scaleX, scaleY, rotation, color) {
        const { buffer, vertexCount } = this.buffers.get(type);

        this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);

        // Re-enable the vertex attribute array
        const positionLocation = this.gl.getAttribLocation(this.program, 'a_position');
        this.gl.enableVertexAttribArray(positionLocation);
        this.gl.vertexAttribPointer(positionLocation, 2, this.gl.FLOAT, false, 0, 0);

        // Setup the color
        const colorLocation = this.gl.getUniformLocation(this.program, 'u_color');
        this.gl.uniform4fv(colorLocation, color);

        const matrixLocation = this.gl.getUniformLocation(this.program, 'u_matrix');
        // Create transformation matrices directly in pixel space
        const translationMatrix = createTranslationMatrix(x, y);
        const rotationMatrix = createRotationMatrix(rotation);
        const scalingMatrix = createScalingMatrix(scaleX, scaleY);

        // Combine them: translationMatrix * rotationMatrix * scalingMatrix
        const combinedMatrix = multiplyMatrices(rotationMatrix, multiplyMatrices(translationMatrix, scalingMatrix));

        // Pass the transformation matrix to the shader
        this.gl.uniformMatrix3fv(matrixLocation, false, combinedMatrix);

        this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, vertexCount);
    }
}

Meine Frage ist, warum mein Quadrat korrekt im Pixel-Space gerendert wird, während die draw-Operation in meinem VertexManager Clip-Space zeichnet, obwohl beide denselben Shader verwenden. Ich habe das Problem schon seit Stunden versucht zu lösen. Hat jemand eine Lösung oder einen Hinweis, was ich möglicherweise falsch mache?

Bild zum Beitrag
JavaScript, Code, OpenGL, Webentwicklung

Code optimieren?

Huhu ich hab's endlich geschafft meinen eigenen Code zu schreiben so dass er funktioniert und ich diesen auch verstehe. Jetzt ist meine nächstes "Problem" die optimierung kann man diesen in kürzerer Form schreiben?

```js

const array = ["3","4","5","6","7"];

for (let i= 0; i<=15; i++){

 console.log(array[0].repeat(20-i)+array[1].repeat(i));

}

for (let i= 16; i<=20; i++){

 console.log(array[0].repeat(20-i)+array[1].repeat(i));

 console.log(array[0].repeat(20-i)+array[1].repeat(i));

}

for (let i= 21; i<=35; i++){

 console.log(array[1].repeat(40-i)+array[2].repeat(i-20));

}

for (let i= 36; i<=40; i++){

 console.log(array[1].repeat(40-i)+array[2].repeat(i-20));

 console.log(array[1].repeat(40-i)+array[2].repeat(i-20));

}

for (let i= 41; i<=55; i++){

 console.log(array[2].repeat(60-i)+array[3].repeat(i-40));

}

for (let i= 56; i<=60; i++){

 console.log(array[2].repeat(60-i)+array[3].repeat(i-40));

 console.log(array[2].repeat(60-i)+array[3].repeat(i-40));

}

for (let i= 61; i<=75; i++){

 console.log(array[3].repeat(80-i)+array[4].repeat(i-60));

}

for (let i= 76; i<=80; i++){

 console.log(array[3].repeat(80-i)+array[4].repeat(i-60));

 console.log(array[3].repeat(80-i)+array[4].repeat(i-60));

}

```

sry hab kein plan wie man hier code formatiert rein haut^^ und danke für die geduld und fürs erklären. PS ich hoffe das kürzere ergebnis wird nicht zu kryptisch weil ich es dann wenn ich das in 6 wochen nochmal anschaue nicht mehr lesen kann. Dankö und habt einen schönen Start in die Woche🌞✌🏼

Computer, IT, programmieren, JavaScript, Array, optimierung, Webentwicklung, For-Schleife

Meistgelesene Beiträge zum Thema JavaScript