Skip to content

Instantly share code, notes, and snippets.

@ivanteoh
Created July 6, 2020 11:26
Show Gist options
  • Save ivanteoh/fb8b315996c0be7cf3184f4c1d17c417 to your computer and use it in GitHub Desktop.
Save ivanteoh/fb8b315996c0be7cf3184f4c1d17c417 to your computer and use it in GitHub Desktop.
Javascript: 101 Week 3 Track 1
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function reduce(combine, base, array) {
forEach(array, function (element) {
base = combine(base, element);
});
return base;
}
function zeroes(a, b) {
if (b) {
return a;
}
return a + 1;
}
function countZeroes(numbers) {
return reduce(zeroes, 0, numbers);
}
document.write(countZeroes([1, 3, 0, 6, 0, 0, 0])); // 4
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function count(array, test) {
total = 0;
forEach(array, function (element) {
if (test(element)) {
total += 1;
}
});
return total;
}
function zeroes(a) {
return !a;
}
function countZeroes(numbers) {
return count(numbers, zeroes);
}
document.write(countZeroes([1, 3, 0, 6, 0, 0, 8, 2])); // 3
function processParagraph(paragraph) {
// check whether this paragraph is header
var typeString = 'p';
var header = 0;
for (var i = 0; i < paragraph.length; i++) {
if (paragraph.charAt(i) == '%') {
header += 1;
} else {
break;
}
}
if (header) {
typeString = 'h' + header;
content = paragraph.substr(header + 1);
} else {
content = paragraph.substr(header);
}
return {'content': content, 'type': typeString};
}
// {'content': 'Language', 'type': 'h2'}
console.log(processParagraph("%% Language"));
// {'content': 'A hermit spent ten years writing a program.', 'type': 'p'}
console.log(processParagraph("A hermit spent ten years writing a program."));
// {'content': 'The Book of Programming', 'type': 'h1'}
console.log(processParagraph("% The Book of Programming"));
function splitParagraph(paragraph) {
var paragraphs = [];
function spliting(first, last, typeString) {
while (true) {
start = paragraph.indexOf(first, 0);
if (start == -1) {
break;
}
end = paragraph.indexOf(last, start + 1);
if (end == -1) {
break;
}
// content does not have "*"
content = paragraph.slice(start, end + 1).slice(1, -1);
paragraph = paragraph.slice(0,
paragraph.charAt(start) == " "? start - 1 :start)
.concat(paragraph.slice(end + 1));
paragraphs.push({'content': content, 'type': typeString});
}
}
// emphasised part
spliting("*", "*", "emphasised");
// footnote
spliting("{", "}", "footnote");
// normal text
paragraphs.push({'content': paragraph, 'type': 'normal'});
return paragraphs;
}
// [{'content': 'million', 'type': 'emphasised'},
// {'content': 'mine is almost a lines', 'type': 'normal'}]
console.log(splitParagraph("mine is almost a *million* lines"));
// [{'content': 'Type something!', 'type': 'emphasised'},
// {'content': "and shouted '' The student", 'type': 'normal'}]
console.log(splitParagraph("and shouted '*Type something!*' The student"));
// [{'content': 'square', 'type': 'emphasised'},
// {'content': 'result', 'type': 'emphasised'},
// {'content': 'two and then it again, the is already inaccurate!',
// 'type': 'normal'}]
console.log(splitParagraph("two and then *square* it again, the *result* is " +
"already inaccurate!"));
// [{'content': 'million', 'type': 'footnote'},
// {'content': 'mine is almost a lines', 'type': 'normal'}]
console.log(splitParagraph("mine is almost a {million} lines"));
// [{'content': 'square', 'type': 'emphasised'},
// {'content': 'result', 'type': 'footnote'},
// {'content': 'two and then it again, the is already inaccurate!',
// 'type': 'normal'}]
console.log(splitParagraph("two and then *square* it again, the {result} is " +
"already inaccurate!"));
function tag(name, content, attributes) {
return {name: name, attributes: attributes, content: content};
}
function image(path) {
return tag("img", [], {src: path});
}
function footnote(number) {
return tag("sup", [link("#footnote" + number,
String(number))]);
}
function splitParagraph(paragraph) {
var paragraphs = [];
function spliting(first, last, typeString) {
while (true) {
start = paragraph.indexOf(first, 0);
if (start == -1) {
break;
}
end = paragraph.indexOf(last, start + 1);
if (end == -1) {
break;
}
// content does not have "*"
content = paragraph.slice(start, end + 1).slice(1, -1);
paragraph = paragraph.slice(0,
paragraph.charAt(start) == " "? start - 1 :start)
.concat(paragraph.slice(end + 1));
paragraphs.push({'content': content, 'type': typeString});
}
}
// emphasised part
spliting("*", "*", "emphasised");
// footnote
spliting("{", "}", "footnote");
// normal text
paragraphs.push({'content': paragraph, 'type': 'normal'});
return paragraphs;
}
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function map(func, array) {
var result = [];
forEach(array, function (element) {
result.push(func(element));
});
return result;
}
function tag(name, content, attributes) {
return {name: name, attributes: attributes, content: content};
}
function link(target, text) {
return tag("a", [text], {href: target});
}
function footnote(number) {
return tag("sup", [link("#footnote" + number,
String(number))]);
}
function renderFragment(fragment) {
var content;
switch (fragment.type) {
case "emphasised":
content = tag("em", [fragment.content]);
break;
case "footnote":
content = footnote(fragment.content);
break;
//case "normal":
default:
content = fragment.content;
break;
}
return content;
}
// {'content': 'A hermit spent ten years writing a program.', 'type': 'p'}
// {'content': 'two and then *square* it again, the {result} is already
// inaccurate!', 'type': 'p'}
function renderParagraph(paragraph) {
// [{'content': 'square', 'type': 'emphasised'},
// {'content': 'result', 'type': 'footnote'},
// {'content': 'two and then it again, the is already inaccurate!',
// 'type': 'normal'}]
fragments = splitParagraph(paragraph.content);
return tag(paragraph.type, map(renderFragment, fragments));
}
console.log(renderParagraph({'content': 'two and then *square* it again, the ' +
'{result} is already inaccurate!', 'type': 'p'}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment