Created
July 6, 2020 11:10
-
-
Save ivanteoh/7b793ec435d3a1c1c97f1cd65b410507 to your computer and use it in GitHub Desktop.
Javascript: 101 Week 2 Track 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cat = {}; // a 'set' of names | |
cat['Red Lion'] = {'colour': 'grey', 'size': 46}; // a name is added to this set | |
cat['Doctor Hobbles'] = {'colour': 'white', 'size': 15}; | |
cat['Little Iroquois'] = {'colour': 'yellow', 'size': 30}; | |
delete cat['Doctor Hobbles']; // a name is removed from this set | |
if ('Red Lion' in cat) { // check whether a name occurs in this set. | |
alert('you have "Red Lion" cat.'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(num, start) { | |
var list = []; | |
if (arguments.length < 2) { | |
for (var i = 0; i <= num; i++) { | |
list[i] = i; | |
} | |
} else { | |
for (var x = 0, y = num; y <= start; x++, y++) { | |
list[x] = y; | |
} | |
} | |
return list; | |
} | |
document.write(range(5)); // 0, 1, 2, 3, 4, 5 | |
document.write(range(2, 5)); // 2, 3, 4, 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(num, start) { | |
var list = []; | |
if (arguments.length < 2) { | |
for (var i = 0; i <= num; i++) { | |
list[i] = i; | |
} | |
} else { | |
for (var x = 0, y = num; y <= start; x++, y++) { | |
list[x] = y; | |
} | |
} | |
return list; | |
} | |
function sum(numbers) { | |
result = 0; | |
for (var i = 0; i < numbers.length; i++) { | |
result += numbers[i]; | |
} | |
return result; | |
} | |
document.write(sum(range(1, 10))); // 55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(num) { | |
var list = []; | |
for (var i = 0; i <= num; i++) { | |
list[i] = i; | |
} | |
return list; | |
} | |
document.write(range(5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sentence = ["We are", "family"]; | |
sentence.join(" ").split(" "); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function startsWith (firstString, secondString) { | |
var result = false; | |
firstLength = firstString.length; | |
secondLength = secondString.length; | |
if (secondLength > firstLength) | |
return result; | |
if (firstString.slice(0, secondLength) == secondString) | |
result = true; | |
return result; | |
} | |
document.write(startsWith("oranges", "orange")); // true | |
document.write(startsWith("peach", "orange")); // false | |
document.write(startsWith("peaches", "grape")); // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function catNames(paragraph) { | |
var result = []; | |
var colon = paragraph.indexOf(":"); | |
allCat = paragraph.slice(colon + 1); | |
names = allCat.split(","); | |
for (var i = 0; i < names.length; i++) { | |
var catName = names[i].split(""); | |
var startName; | |
var endName; | |
// Trims whitespace from the left side of the string. | |
for (var j = 0; j < catName.length; j++) { | |
startName = j; | |
if (catName[j] != " ") | |
break; | |
} | |
// Trims whitespace from the right side of the string. | |
for (var k = catName.length - 1; k > -1; k--) { | |
endName = k; | |
if (catName[k] != " ") | |
break; | |
} | |
result.push(names[i].slice(startName, endName+1)); | |
} | |
return result; | |
} | |
var paragraph = "born 05/04/2006 (mother Lady Penelope): Red Lion, Doctor" + | |
"Hobbles the 3rd, Little Iroquois"; | |
document.write(catNames(paragraph)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extractDate(paragraph) { | |
var colon = paragraph.indexOf(":"); | |
var space = paragraph.indexOf(" "); | |
var allDate = paragraph.slice(space + 1, colon); | |
var dates = allDate.split("/"); | |
var day = dates[0]; | |
var month = dates[1]; | |
var year = dates[2]; | |
var result = new Date(year, month-1, day); | |
return result; | |
} | |
var paragraph = "died 27/04/2006: Black Leclère"; | |
document.write(extractDate(paragraph)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function between(paragraph, firstPattern, secondPattern) { | |
var start = paragraph.indexOf(firstPattern) + firstPattern.length; | |
var end = paragraph.indexOf(secondPattern, start); | |
return paragraph.slice(start, end); | |
} | |
document.write(between("born 15/11/2003 (mother Spot): White Fang", | |
"(mother ", ")")); // "Spot" | |
document.write(between("bu ] boo [ bah ] gzz", "[ ", " ]")); // "bah" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formatDate(date) { | |
var month = date.getMonth() + 1; | |
var day = date.getDate(); | |
return ((day < 10)? ("0" + day) : day) + | |
"/" + ((month < 10)? ("0" + month) : month) + | |
"/" + date.getFullYear(); | |
} | |
document.write(formatDate(new Date(2010, 11, 25))); // "25/12/2010" | |
document.write(formatDate(new Date(2011, 1, 4))); // "04/02/2011" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function oldestCat(cats) { | |
var cat; | |
var oldest; | |
for (var name in cats) { | |
if (!("death" in cats[name]) && !oldest || (oldest > cats[name].birth)) | |
{ | |
oldest = cats[name].birth; | |
cat = cats[name].name; | |
} | |
} | |
return cat; | |
} | |
var cats = {"Spot": {name:"Spot", birth: new Date(1997, 2, 5), | |
mother: "Yellow"}, | |
"White": {name:"White", birth: new Date(1998, 7, 4), | |
mother: "Zebra"}, | |
"Grey": {name:"Grey", birth: new Date(1996, 6, 3), | |
mother: "Kitten"}}; | |
document.write(oldestCat(cats)); // "Grey" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment