JSFiddle

  • Equilibrium Index #2 is the latest revision

    Write a function: function solution(A); that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

    function solution(A) {
        var rside = 0;
        var lside = 0;
        var eqTest = -1;
        
        if (A === null) {
            return eqTest;
        } else if (A ...
  • Determine whether a triangle can be built from a given set of edges #26 is the latest revision

    By sorting the array, we have guaranteed that P+R < Q and Q+R < P (because R is always the biggest). Now what remains, is the proof that P+Q > R, that can be found out by traversing the array. The chance to find such a combination is with three adjacent values as they provide the highest P and Q.

    var A = [10,2,5,1,8,20]; // not possible  ... possible eg [10,2,5,1,11,20]
    var B ...
  • What Split in Array Yields Min Value

    Write a function that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.

    var A = [3,1,2,4,3];
    var rside = 0;
    var lside = 0;
    var diff = 0;
    var min = Number.MAX_VALUE ...
  • Frog Jump Problem #13 is the latest revision

    A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its target.

    function frogMinJumps(x,y,d) {
    	 var numberOfJumps;
       // Assume x,yand d are 1 .. 1,000,000,000
       if (x < 1 || y < 1 || d < 1) {
    	   return 'Values must be greater than 1';
       }
       if (x > 1000000000 ...
  • Find Missing Element

    Given an integer set from 1 to n, calculate the missing integer.

    var set = [2,3,4,5,6,7,8,9];
    var result = document.querySelector('#Result');
    document.querySelector('#Set').textContent = set ...
  • isPermutation

    Check whether array N is a permutation

    /*  
       Elements seen twice or out of bounds of the size indicate that the list is no permutation. 
       This solution works ...
  • flatten a non standard array

    No-Library (pure JS), HTML, CSS, JavaScript

    // No Cigar
    //var flattened = [1,2,3,[4,5,6],7,8].reduce(function(a, b) {
    //    if(Array.isArray(b ...
  • Translate in CSS

    No-Library (pure JS), HTML, CSS, JavaScript

    <div id="wrapper">
        <div id="box">
            <img src="http://startupnorth.ca/wp-content/uploads/2013/12/unicor1.jpg" width="100" height="auto"/>
        </div>
    </div>
  • Swapping Variable Methods

    Some methods for swapping values.

    var A = 10;
    var B = 5;
    console.log(B ^= (A ^ (A=B)));  // XOR using bitwise operator
    console.log(A);
    console ...
  • Prime Time

    Write a function to take the num parameter and return true if it is a prime number or false if it composite.

    var element = window.document.getElementById('prime');
    var output = window.document.getElementById('output');
    function primeTime(e) {
        e.preventDefault();
        var selector = '#' +e ...