ALGORITHMS

ALGORITHMS

by SHELDON PASCIAK

HTML

<div id="banner-message">
  <button>ok</button>
  <div id="result">  
  </div>
</div>

CSS

/*

Josiah   6:56 PM
I snagged Given a string, return the character that is most
commonly used in the string.
--- Examples
maxChar("abcccccccd") === "c"
maxChar("apple 1231111") === "1"

*/

JavaScript

// find elements
var banner = $("#banner-message")
var button = $("button")

// notes -- 

/*
 Sheldon and Josiah solution was a slow attempt at just 
 solving the problem and not analyzing the optimal way
 mostly focused on getting the language (code) to do it.
 
 After more thought, code isn't the importance the logic
 and awareness of the WHY, and how many, Big O type stuff
 is necessary.

 
 The following is our quick solution but very heavy cycle and memory use
 
 Is the goal to write it, or to really think it in 30 minutes ?
 
 Note, our result returns any/all that match the highest count compared
 to the internet solution.
 
 I've spent more time than should on this though.
 
*/

function ourFirstAttempt(x) { 

 $('#result').append("<br><br>Our answer<br><br>");
 
   $('#result').append("<br>param:'" + x + "'<BR><BR>");
 
// find a place for totals
  let values = [];

// set totals to empty
  for(var z = 0; z<256; z++) {
  	values [z] = 0; 
  }

// for each character in string, find the storage location
// increase value at the storage location
  for(i=0; i<x.length; i++) { 
    let y = x.charAt(i).charCodeAt(0);
    values[y]++; 
  } 

// find the value in storage that has the highest count
  currentHighestCount = 0 ;
	values.forEach(element => {
    if (element > currentHighestCount){
      currentHighestCount = Math.floor(element);
    } 
  })  

	// show the characters that are in the positions that have highest counts
  for (var i=0;i<256;i++){

    if (values[i]>=currentHighestCount) {
      $('#result').append("<br>"+("'"+String.fromCharCode(i) + "'"+":"+ currentHighestCount+"<br>"));
    }
}

}
 
 
 /*

The following internet answer uses its version of a dictionary
to hash the totals into spots key'ed by the characters

It makes one run through the array and captures all the 
info it needs in that one iteration
*/
function internetAnswer(exp) { 
  var expCounts = {};
  var maxKey = '';
  for(var i = 0; i < exp.length; i++)
  {
      var key =...