Skip to content

Instantly share code, notes, and snippets.

@GSKang94
Last active June 28, 2021 06:31
Show Gist options
  • Save GSKang94/edec3e5fcec6151c607ae5c3a55553ed to your computer and use it in GitHub Desktop.
Save GSKang94/edec3e5fcec6151c607ae5c3a55553ed to your computer and use it in GitHub Desktop.
Eloquent javaScript (3rd ed.) exercises
// Write loop That make seven calls to console.log to display a pyramid of # (Page 37)
let triangle = '';
for (let i = 0; i < 7; i++) {
triangle += '#';
console.log(triangle);
}
//Print numbers from 1 to 100. For numbers divisible by 3, print Fizz ; For numbers divisible by 5, print Buzz; For numbers divisible by both, print FizzBuzz (Page 37)
let result = '';
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
result = 'FizzBuzz';
} else if (i % 3 === 0) {
result = 'Fizz';
} else if (i % 5 === 0) {
result = 'Buzz';
} else {
result = i;
}
console.log(result);
}
//Create 8 X 8 Chess board (Page 38)
const chessBoard = (size) => {
let square = '';
for (let i = 1; i <= size * size; i++) {
if (i % size === 0) {
square += '#\n';
} else if (i % 2 === 0) {
square += '#';
} else {
square += ' ';
}
}
console.log(square);
};
chessBoard(8);
//create a fn to mimic Math.min method (Page 56)
const findMin = (a, b) => {
const smallerNum = a < b ? a : b;
return smallerNum;
};
findMin(3, 4);
// Recursive fn to check even or odd nums (Page 56)
const isEven = (num) => {
if (num === 0) {
return true;
} else if (num === 1) {
return false;
} else {
return isEven(num - 2);
}
};
isEven(13);
// Fn to count a given character in a string (Page 56)
const countChar = (string, character) => {
const strToArr = string.split('');
const filterChar = strToArr.filter((char) => char === character);
return filterChar.length;
};
countChar('gagan', 'a');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment