Return to JUNTO

JUNTO Practice: Eloquent JavaScript, Chapter 3

Discussed on December 03, 2019.

Eloquent Javascript

Chapter 3 exercises:

Solutions

Click to see:

Oscar Martinez

Minimum

min = (a,b) => {
  return a < b ? a:b;
}

Recursion

isEven = (n) => {
  n = n < 0 ? -n : n; 
  if (n > 1){
    return isEven(n-2);
  } else if (n===0){
    return true;
  } else if (n===1){
    return false;
  }
}

Bean Counting

countChar = (targetString, char) => {
  charCount = 0;
for (var i = 0; i < targetString.length; i++){
    if (targetString[i]===char){
charCount++;        
        }
    }
  return charCount;
}

countBs = (targetString) => {
return countChar(targetString,"B");
}

John Lekberg

Minimum

const min = (x, y) => x < y ? x : y;

Recursion

const isEven = n => {
  if (n < 0) return isEven(-n);

  if (n === 0) return true;
  if (n === 1) return false;
  
  return isEven(n - 2);
};

Bean Counting

const countChar = (s, c) => {
  let count = 0;
  for (let x of s) {
    if (x === c) {
        count++;
    }
  }
  return count;
};

const countBs = s => countChar(s, "B");

Daniel Bassett

Minimum

var min = function(x, y) {
    return (x <= y) ? x : y;
};

Recursion

var isEven = function(num) {
    num = Math.abs(num); 
    if (num === 0)
        return true;
    else if (num === 1)
        return false;
    else
        return isEven(num - 2);
};

Bean Counting

function countBs(str) {
  return countChar(str, 'B');
}

function countChar(str, chr) {
  var counter = 0
  for (var i = 0; i < str.length; i++) {
    if (str.charAt(i) === chr) {counter++;}
  }
  return counter;
}