Return to JUNTO

JUNTO Practice: Eloquent JavaScript, Chapter 5 Exercises

Discussed on February 25, 2020.

https://eloquentjavascript.net/05_higher_order.html

Exercises:

Solutions

Click to see:

Oscar Martinez

"Flattening".

let arrays = [[1, 2, 3], [4, 5], [6]];
// Your code here.
// → [1, 2, 3, 4, 5, 6]
flattenedArray = arrays.reduce((a,b) => a.concat(b));
console.log(flattenedArray);

"Your Own Loop".

// Your code here.

loop = (value,testFunction,updateFunction,bodyFunction) => {
if (testFunction(value)) {
      bodyFunction(value);
      loop(updateFunction(value),testFunction,updateFunction,bodyFunction);
    }
}

loop(3, n => n > 0, n => n - 1, console.log);
// → 3
// → 2
// → 1

"Everything".

function every(array, test) {
  // Your code here.
  for (i = 0 ; i < array.length ; i++){
    if (!test(array[i])) {
    return false
    }
  }
  return true
}

console.log(every([1, 3, 5], n => n < 10));
// → true
console.log(every([2, 4, 16], n => n < 10));
// → false
console.log(every([], n => n < 10));
// → true

"Dominant Writing Direction".

function characterScript(code) {
  for (let script of SCRIPTS) {
    if (script.ranges.some(([from, to]) => {
      return code >= from && code < to;
    })) {
      return script;
    }
  }
  return null;
}

function countBy(items, groupName) {
  let counts = [];
  for (let item of items) {
    let name = groupName(item);
    let known = counts.findIndex(c => c.name == name);
    if (known == -1) {
      counts.push({name, count: 1});
    } else {
      counts[known].count++;
    }
  }
  return counts;
}

function dominantDirection(text) {
  // Your code here.
  let counts = countBy(text, char => {
    let script = characterScript(char.codePointAt(0));
    return script ? script.direction : "none";
  }).filter(({name}) => name != "none");
  console.log(counts);
  return counts.reduce((a,b) => {
  return b.count > a.count ? b : a
  }).name;
}

console.log(dominantDirection("Hello!"));
// → ltr
console.log(dominantDirection("Hey, مساء الخير"));
// → rtl

John Lekberg

"Flattening".

let arrays = [[1, 2, 3], [4, 5], [6]];
const flattened = arrays.reduce((acc, arr) => acc.concat(arr), []);

"Your Own Loop".

const loop = (init, test, update, f) => {
  let x = init;
  while (test(x)) {
    f(x);
    x = update(x);
  }    
};
loop(3, n => n > 0, n => n - 1, console.log);

"Everything". Using a loop:

function every(array, test) {
  let result = true;
  for (let x of array) {
    result = result && test(x);
  }
  return result;
}
console.log(every([1, 3, 5], n => n < 10));
console.log(every([2, 4, 16], n => n < 10));
console.log(every([], n => n < 10));

Version using some method:

function every(array, test) {
  return !array.some(x => !test(x));
}

"Dominant Writing Direction".

function dominantDirection(text) {
  function* data() {
	for (let char of text) {
	  const scriptData = characterScript(char.charCodeAt(0));
      if (scriptData !== null) {
        yield scriptData.direction;
      }
    }
  }
  const freq = [...frequency(data()).entries()];
  freq.sort(([ka, va], [kb, vb]) => vb - va);
  const [dominant, _] = freq[0];
  return dominant;
}

function frequency(data) {
  const table = new Map();
  for (let datum of data) {
    const currentCount = table.has(datum) ? table.get(datum) : 0;
    table.set(datum, currentCount + 1);
  }
  return table;
}


console.log(dominantDirection("Hello!"));
console.log(dominantDirection("Hey, مساء الخير"));