9.18 Study Log

Tonight I did 4.5h of work on some js Intermediate Algo Scripting, some 10 days of js (4 days finished today!) and some serious note taking. As per the idea, here is the code I wrote today. comments in comments, mostly. I have also started trying to estimate the big-O complexity of my algos. I have a lot of practicing to do, but this is it!

Smallest Common Multiple (2 versions) you’ll see why below.

function smallestCommons(arr) {

  sort numbers, lowest first
  make array of numbers from low to high
  start at checkVal of 1
  in while loop: 
    if array.filter(each element % checkVal returns true).length == array.length
      return number

  let iterArray=[];
  let valFound=false;
  let checkVal=1;
  let iterArrayLength;

  arr.sort(function(a, b){return a-b});// sort numerically. how does this work?
  let filteredArray=[]////  O(n)

  for (let i=arr[0];i<=arr[1];i++){ //make array of all numbers to check

  iterArray.sort(function(a, b){return b-a});// sort numerically. how does this work?
// console.log(iterArray)
  let multipleFound=false
  let testPassed=true
  // version 3
  do { // while loop - caution! O(1)
    checkVal++ // while loop increment
    // console.log("checkVal= "+checkVal)
    for (var i = 0, len = iterArrayLength; i < len; i++) {
      // console.log(" testing   " + iterArray[i])
      if (checkVal % iterArray[i] !== 0){
        // console.log(checkVal % iterArray[i])
    }// console.log(i)
    if (testPassed==true){
      // console.log("hi")
    return checkVal
    } while (multipleFound==false)
  // console.log('do loop exited')
  // version 2 - this works for 5/6 tests but is too slow for the final test.
  // while (iterArrayLength != filteredArray.length){ // while loop - caution! O(1)
  //   checkVal++ // while loop increment
  //   filteredArray=iterArray.filter(number => checkVal % number == 0) //// O(n)
  // }

  // version 1 - slower than version 2
  //   while (valFound!=true){ // while loop - caution! O(1)
  //   filteredArray=iterArray.filter(number => checkVal % number == 0) //// O(n)
  //   if (iterArray.length == filteredArray.length){ //// O(1)
  //     valFound = true; // while loop exit
  //   } else {
  //     checkVal++ // while loop increment
  //   }

  // }
  return checkVal

// smallestCommons([1,5]);
smallestCommons([18, 23]);

Above is my code, written from scratch. I struggled with optimization on this one because my script kept timing out. not when run in dev mode, but in the fcc ide it would never run long enough to pass the final tesk. That’s why all of the logging is commented out. I tried to rewrite the inner loop 3x, but didn’t end up with any meaningful improvement.

I looked at the hints, then the code. I’m impressed with even the simple answer. This is the example code from fcc below. it is interesting and has lots of optimizations that I haven’t considered. for instance it starts the counter at the product of the two largest elements in the array. it also increases the counter by multiplying the previous number by an incrementing counter. It breaks out of the search loop when it finds an array element that does not fit into the search number. this means it is a much faster operation than mine. It is also interesting that even this example code does not pass the tests if it has a console.log in the inner loop. Even my slow code would get the right answer outside of the fcc dev environment, it would just take a long time.

function smallestCommons(arr) {
  // Sort array from greater to lowest
  // This line of code was from Adam Doyle (http://github.com/Adoyle2014)
  arr.sort(function(a, b) {
    return b - a;

  // Create new array and add all values from greater to smaller from the
  // original array.
  var newArr = [];
  for (var i = arr[0]; i >= arr[1]; i--) {

  // Variables needed declared outside the loops.
  var quot = 0;
  var loop = 1;
  var n;

  // Run code while n is not the same as the array length.
  do {
    quot = newArr[0] * loop * newArr[1]; // start search at the multiple of the two biggest numbers, then multiply that by something until all elements fit inside of it.
    console.log(quot) //this breaks the freecodecamp interactive window. sigh.
    for (n = 2; n < newArr.length; n++) { // this starts at 2 because 0 and 1 are already included above
      if (quot % newArr[n] !== 0) {
        break; // this break gets us outside of the for loop, but not the do while loop.
  } while (n !== newArr.length);

  return quot;

Drop it
Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function funcreturns truewhen the iterated element is passed through it.
Then return the rest of the array once the condition is satisfied, otherwise, arrshould be returned as an empty array.
function dropElements(arr, func) {
do a for loop on arr testing against the passed in function
if true, return i from the loop
if no match found, return -1 at end
return arr.slice(i) or [] based on what is returned.

    for (let i=0;i<arr.length;i++){
        if (func(arr[i])==true){
            return arr.slice(i)
        } else {
    return []
dropElements([1, 2, 3], function(n) {return n < 3; });
dropElements([1, 2, 3, 4], function(n) {return n >= 3;});

what I learned
this was one of the problems that most quickly transitioned from strategy to passing tests.
I didn't need to get fancy with foreach or ternary operations. for worked fine.


flatten nested arrays into one array.

function steamrollArray(arr) {
iterate through the array (probably recursively)
look for elements and subarrays
add elements to outArray
run same program on arrays
    function iron(array){
        let outArray=[]
        for (let i=0;i<array.length;i++){
            if (Array.isArray(array[i])==true){
            } else {
        return outArray
    let myOutput=iron(arr)
    return myOutput;

steamrollArray([1, [2], [3, [[4]]]]);
// running tests
steamrollArray([[["a"]], [["b"]]]) //should return ["a", "b"].
// steamrollArray([1, [2], [3, [[4]]]]) //should return [1, 2, 3, 4].
// steamrollArray([1, [], [3, [[4]]]]) //should return [1, 3, 4].
// steamrollArray([1, {}, [3, [[4]]]]) //should return [1, {}, 3, 4].
// tests completed

what I learned
I was just "..." away from functional code, but it took me a long time to realize that.
it's really annoying how hard it is to preview array structure in both chrome and the fcc ide
this mostly went according to plan
I used Array.isArray(element) for the second time
I sucessfully set up recursion! Not too-too-many crashes...

Binary Agents

Convert a string of binary numbers into text.

function binaryAgent(str) {
split on whitespace
figure out binary
make letters out of numbers //String.fromCharCode
make string
return string
    let input=str.split(/\s/);
    for (let i=0;i<input.length;i++) { // O(n) linear time

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

what I learned
a little bit about binary tools
String.fromCharCode(parseInt(input[i],2).toString(10)) looks like a lot to convert binary to char.
This worked mostly according to plan. My biggest stumble was forgetting that \w is not whitespace, not remembering to return anything, and a typo in my for loop


Tell me what you think.

This site uses Akismet to reduce spam. Learn how your comment data is processed.