Tuesday, October 4
Shadow

Tag: JavaScript

PERULANGAN 2

gist
void main() { // soal perulangan while: // 1. buat sebuah perulangan while // 2. buat perulangan while dengan persyaratan : // a. buat variabel dengan nama 'uang' tipe data int // b. beri nilai awal variabel uang = 15000 // c. beri kondisi jika uang lebih dari 1000 // d. beri operasi uang yang sekarang dikurangi 4000 // e. buat baris kode perulangan yang mengeprint di console : // 'saya jajan batagor, uang saya x' // f. ganti kata x dengan variabel uang // 3. klik run // soal perulangan do .. while: // 1. buat sebuah perulangan do .. while // 2. buat perulangan do .. while dengan persyaratan : // a. buat variabel dengan nama 'umur' tipe data int // b. beri nilai awal variabel umur = 16 // ...

Solution to level 11 in Untrusted:

gist
/* * robot.js * * You'll need three keys in order to unlock the * Algorithm: the red key, the green key, and the * blue key. Unfortunately, all three of them are * behind human-proof barriers. * * The plan is simple: reprogram the maintenance * robots to grab the key and bring it through * the barrier to us. * * Let's try it on the red key first. */ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function startLevel(map) { // Hint: you can press R or 5 to "rest" and not move the // player, while the robot moves around. map.placePlayer(map.getWidth()-2, map.getHeight()-2); var player = map.getPlayer(); map.defineObject('robot', { 'type': 'dynamic', 'symbol': 'R', 'color': 'gray', ...

Lua script to delete/trim all processed messages from a Redis stream

gist
-- The goal of this script is to trim messages that have been processed by -- all extant groups from the a given Redis stream. It returns the number -- of messages that were deleted from the stream, if any. I make no -- guarantees about its performance, particularly if the stream is large -- and not fully processed (so a simple XTRIM isn't possible). -- First off, bail out early if the stream doesn't exist. if redis.call("EXISTS", KEYS[1]) == 0 then return false end -- To figure out what messages are deletable, we fetch the "last- -- delivered-id" for each consumer group of the stream, and set the lowest -- one of those ids as our upper bound. Next, we scan the pending lists -- for each group, because we also don't want to delete any events that -- are delivered but not acknowledg...

Example implementation of gauss elimination in javascript

gist
function print(M, msg) { console.log("======" + msg + "=========") for(var k=0; k<M.length; ++k) { console.log(M[k]); } console.log("==========================") } function diagonalize(M) { var m = M.length; var n = M[0].length; for(var k=0; k<Math.min(m,n); ++k) { // Find the k-th pivot i_max = findPivot(M, k); if (A[i_max, k] == 0) throw "matrix is singular"; swap_rows(M, k, i_max); // Do for all rows below pivot for(var i=k+1; i<m; ++i) { // Do for all remaining elements in current row: var c = A[i][k] / A[k][k]; for(var j=k+1; j<n; ++j) { A[i][j] = A[i][j] - A[k][j] * c; } // Fill lower triangular matrix with zeros A[i][k]...

How `this` works in javascript

gist
this binding this is the runtime context of a function. this is determined by the call site the same function can be executed with different this runtime contexts. You can think of this as another arguement to the function Comparison: scopes are generally defined at compile time (exception: eval) const person = { name: 'Alex', getName() { return this.name; } } const foo = person.getName; // using default binding (`undefined` in strict mode) foo(); // using explicit binding person.getName.call({name: 'Ben'}); // using implicit binding // using `person` as the 'this' binding person.getName(); Rules (In order of precedence) new (new Person()) explicit (call, apply, bind) implicit (obj1.foo()) default (window, or undefined in strict mode) Exc...

TypeScript | JavaScript Algorithms | Staircase solution.

gist
// Staircase - HackerHank const BREAK_CHAR = '\n'; const HASH_CHAR = '#'; const WHITESPACE_CHAR = ' '; type StairOptions = { length: number; symbolTotal: number; isLeft?: boolean; } /** * Complete the staircase function in the editor below. * * Staircase has the following parameter(s): * int n: an integer */ function isNotBetweenLength(lnegth: number): boolean { return !(lnegth >= 1 && lnegth <= 100); } function staircase(n: number): string { const isNotContraintsPass = isNotBetweenLength(n); if (isNotContraintsPass) { // Error: "Invalid input/length". return ""; } const arr = Array.from({ length: n }, (_, i) => i + 1); function stairChar({ length, symbolTotal, isLeft }: StairOptions): string { let str: string = ''; const LE...