Ad
Code
Diff
  • function pairs(num_elements, target_value, array) {
      array.sort(function(a,b) { return a - b; });
      
      let num_pairs = 0;
      let j = 1;
      
      for(let i=0; i<array.length-1; i++) {
        while(array[j] - array[i] < target_value && j <array.length-1) {
          j++;
        }
        
        if(array[j] - array[i] === target_value) {
          num_pairs++;
        }
      }
      
      return num_pairs;
    }
    • function pairs(num_elements, target_value, array) {
    • array.sort(function(a,b) { return a - b; });
    • return 0;
    • let num_pairs = 0;
    • let j = 1;
    • for(let i=0; i<array.length-1; i++) {
    • while(array[j] - array[i] < target_value && j <array.length-1) {
    • j++;
    • }
    • if(array[j] - array[i] === target_value) {
    • num_pairs++;
    • }
    • }
    • return num_pairs;
    • }

Set 1 Challenge 1 for the cryptopal crypto challenges (https://cryptopals.com/sets/1/challenges/1).

Convert hex to base64

function hexToBase64(hex) {
  return hex;
}

Some user interactions, such as resizing and scrolling, can create a huge number of browser events in a short period of time. If listeners attached to these events take a long time to execute, the user's browser can start to slow down significantly. To mitigate this issue, we want to to implement a throttle function that will detect clusters of events and reduce the number of times we call an expensive function.

Your function will accept an array representing a stream of event timestamps and return an array representing the times that a callback should have been called. If an event happens within wait time of the previous event, it is part of the same cluster. Your function should satisfy the following use cases:

  1. Firing once on the first event in a cluster, e.g. as soon as the window starts resizing.
  2. Firing once after the last event in a cluster, e.g. after the user window stops resizing.
  3. Firing every interval milliseconds during a cluster, e.g. every 100ms while the window is resizing.
function throttle(wait, onLast, onFirst, interval, timestamps) {
  let ret = [];
  let cluster = [timestamps[0]];
  
  for(let i=1; i<timestamps.length; i++) {
    if(timestamps[i] - timestamps[i-1] <= wait) {
      cluster.push(timestamps[i]);
    } else {
      let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
      clusterEventTimes.forEach( function(el){ ret.push(el); });
      cluster = [timestamps[i]];
    }
    
    if(i == timestamps.length-1) {
      let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
      clusterEventTimes.forEach( function(el){ ret.push(el); });
    }
  }
  
  return ret;
}

// Determines all times when an event needs to be fired
function evaluateCluster(wait, onLast, onFirst, interval, cluster){
  let ret = [];
  
  if(onFirst) {
    ret.push(cluster[0]); // push cluster start
  }
  if(interval != 0) {
    let maxInterval = cluster[cluster.length-1];
    if(onLast) {
      maxInterval += wait;
    }
    for(let intEv = cluster[0]+interval; intEv < maxInterval; intEv+=interval) {
      ret.push(intEv);
    }
  }
  if(onLast) {
    ret.push(cluster[cluster.length-1]+wait); // push cluster end
  }
    
  return ret;
}