Computer-Ag am WvS Blog der Computer-AG am Werner von Siemens Gymnasium Berlin

3. Mai 2019

3.5.2019

Filed under: Allgemein — admin @ 13:37

Jannik und Felix sind da. Wir reden über RAM und vRAM (Video-RAM).

Robert baut ein Summierskript, basierend auf dem Memoizer von Crockford:

<input type="text" id="input">
</input>
<button id="go">go</button>
<div id="output">
</div>
<script>
var memoizer = function (memo, formula) { 
  var recur = function (n) {
    var result = {
          calc:"",
	  notation:""
    }
    result.calc = memo.calc[n]; 
    result.notation = memo.notation[n];
    if (typeof result.calc !== 'number') {
      result = formula(recur, n); 
//~       console.log(result);
      memo.calc[n] = result.calc;
      memo.notation[n] = result.notation;
    } 
    return result;
  }; 
  return recur;
};

var sumUp = memoizer({calc:[0, 1], notation:["", "1"]}, function (recur, n) {
    var result = {
          calc:"",
	  notation:""
    }
  result.calc = parseInt(n,10) + recur(n -1).calc;
  result.notation = n + "+" + recur(n -1).notation;
  return result; 
})

var input = document.getElementById("input");

var button = document.getElementById("go");

var output = document.getElementById("output");

button.onclick = function() {
//	alert(input.value);
       res = sumUp(input.value);
console.log(typeof (input.value | 0));
console.log(res);
       output.innerHTML = res.calc + " = " + res.notation;
}


</script>


Angelo ist in Schweden.

Keine Kommentare »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress