Last active
July 25, 2022 17:56
-
-
Save davejellicoe/7bd55cf6334bd26da01bce82b72e3a92 to your computer and use it in GitHub Desktop.
javascript samples from week 1
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 average(list) { | |
var sum = 0; | |
for (var num in list) { | |
sum += list[num]; | |
} | |
return sum / list.length; | |
} | |
console.log(average([20, 21, 220, 54])); |
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
// Author: Dave J | |
// take array ["gists", "types", "operators", "iteration", "problem solving"] | |
// and out put it with text to say | |
// Today I learned about gists, types, operators, iteration, problem solving. | |
// without a function call | |
// var concepts = ("Today I learned about " + conceptList.toString() + ".").split(',').join(', '); | |
// console.log(concepts); | |
// with a function call | |
function joinList(list){ | |
var concepts = ("Today I learned about " + list.toString().split(',').join(', ') + "."); //; | |
return concepts; | |
}; | |
var conceptList = ["gists", "types", "operators", "iteration", "problem solving"]; | |
joinedList = joinList(conceptList); | |
console.log(joinedList); |
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
// Author Dave J | |
var countdownGenerator = function (x) { | |
var num = x | |
return function() { | |
if (num >= 1) { | |
console.log("T-minus ",num, "..."); | |
num--; | |
return num | |
} | |
else if (num === 0) { | |
console.log('Blast Off!'); | |
num-- | |
return num | |
} | |
else { | |
console.log("Rocket Launched") | |
return num | |
} | |
} | |
} |
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 countLetters(sentence) { | |
var noSpaces = sentence.split(" ").join(""); | |
for (var i = 0; i <= noSpaces.length -1; i++) { | |
var letter = noSpaces[i]; | |
var num = -1; | |
var pos = 0; | |
var j = -1; | |
while (pos != -1) { | |
pos = noSpaces.indexOf(letter, j + 1); | |
num += 1; | |
j = pos; | |
} | |
console.log(letter + ':' + num); | |
} | |
} | |
var result = countLetters('the quick brown fox jumped over the lazy dog'); |
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
// Author: Dave J | |
var date = process.argv[2]; | |
if (!date) { | |
console.log("Please provide a date in the format YYYY/MM/DD"); | |
} else { | |
calculateDayInYear(date); | |
} | |
function calculateDayInYear(date) { | |
var splitDate = date.split('/'); | |
var year = Number(splitDate[0]); | |
var month = Number(splitDate[1]); | |
var day = Number(splitDate[2]); | |
var febDays = daysInFeb(year); | |
var DAYS_IN_MONTH = [0, 31, febDays, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | |
if (year && validMonth(month) && validDay(month, day)) { | |
console.log(date, "is day", calculateDayNumber(month, day), "of the year", year); | |
} else { | |
console.log(day); | |
console.log(month); | |
console.log(year); | |
console.log("Invalid date"); | |
} | |
function validMonth(month) { | |
return month && month > 0 && month < 13; | |
} | |
function validDay(month, day) { | |
return day && day > 0 && day < DAYS_IN_MONTH[month+1]; | |
} | |
function calculateDayNumber(month, day) { | |
var dayOfYear = day; | |
console.log(dayOfYear); | |
for (var i = 1; i <= month; ++i) { | |
dayOfYear += DAYS_IN_MONTH[i-1]; | |
console.log(DAYS_IN_MONTH[i]); | |
} | |
return dayOfYear; | |
} | |
function daysInFeb(year) { | |
if (((year % 100) != 0 || (year % 400) == 0) && (year % 4 == 0)) { | |
return 29; | |
} else { | |
return 28; | |
} | |
} | |
} |
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
// Author: Dave J | |
// node dice-roller.js 3 takes a numeric parameter and creates that many random dices rolls | |
// Rolled 3 dice: 2, 6, 5 | |
function diceroller(num){ | |
for (var i = 0; i <= num-1; i++) { | |
var ranDice = Math.floor(6*Math.random())+1; | |
console.log(ranDice) | |
}; | |
}; | |
diceroller(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 findWaldo(arr, found) { | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] === "Waldo") { | |
found(i); // execute callback with arguments | |
} else { | |
console.log('didnt find him'); | |
} | |
} | |
} | |
function actionWhenFound(index) { | |
console.log("Found him!"); | |
console.log("He was found at Index " + index); | |
} | |
findWaldo(["Alice", "Bob", "Waldo", "Winston"], actionWhenFound); |
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 actionWhenFound(element,index,array) { | |
if (array[index] === "Waldo") { | |
console.log("Found him!"); | |
console.log("He was found at Index " + index); | |
} else { | |
console.log('didnt find him'); | |
} | |
} | |
["Alice", "Bob", "Waldo", "Winston"].forEach(actionWhenFound);// execute callback |
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 loadedDie = (function () { | |
var list = [3, '.', 1, 4, 1, 5, 9, 2, 6]; | |
var dieNum = 0 | |
list[dieNum]; | |
return function () { | |
return list[dieNum++]; | |
} | |
})(); | |
console.log(loadedDie()); // 3 | |
console.log(loadedDie()); // . | |
console.log(loadedDie()); // 1 | |
console.log(loadedDie()); // 4 | |
console.log(loadedDie()); // 1 ... or pi | |
// var rollDie = function (){ | |
// return Math.floor(1 + Math.random() *6); | |
// } | |
// console.log(rollDie()); //real |
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
// Author: Dave J | |
// prints the numbers from 100 to 200 to the console, with three exceptions: | |
// If the number is a multiple of 3, print the String "Loopy". | |
// If the number is a multiple of 4, print the String "Lighthouse". | |
// If the number is a multiple of both 3 and 4, print the String "LoopyLighthouse" | |
for (var i = 100; i <= 200; i++) { | |
var lo = i % 3 == 0; | |
var li = i % 4 == 0; | |
console.log(lo ? li ? "LoopyLighthouse" : "Loopy" : li ? "Lighthouse" : i); | |
} |
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
// Author : Dave J | |
var input = [ | |
{ x: 3, y: 4 }, | |
{ x: 12, y: 5 }, | |
{ x: 8, y: 15 } | |
]; | |
// z = Math.sqrt(input(x)^ + input(y)^); | |
var result = input.map(function(obj){ | |
return Math.sqrt(Math.pow(obj.x, 2) + Math.pow(obj.y, 2) ); | |
}); | |
console.log(result[0] === 5); | |
console.log(result[1] === 13); | |
console.log(result[2] === 17); |
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
// Author DaveJ | |
// implement the following functions on this data | |
// add Library, Tracks, Playlists, | |
// add Playlists to libraries and Tracks to Playlists | |
// calculate properties like total length, average rating | |
// Output any object | |
function Library(name, owner){ | |
this.playlist = [], | |
this.name = name, | |
this.owner = owner | |
} | |
function Playlist(name){ | |
this.name = name, | |
this.tracks = [] | |
} | |
function Track(name,artist,album,rating,length){ | |
this.name = name, | |
this.artist = artist, | |
this.album = album, | |
this.rating = rating, | |
this.length = Number(length) | |
} | |
Library.prototype.addPlaylistToLib = function(playlist) { return this.playlist.push(playlist) } | |
Playlist.prototype.addTrackToPlaylist = function(track) { return this.tracks.push(track) } | |
Object.defineProperty(Playlist.prototype, "duration",{ | |
get: function(){ | |
let totalLength = 0; | |
this.tracks.forEach((track)=>{ | |
totalLength += track.length; | |
}) | |
return totalLength; | |
} | |
}); | |
Object.defineProperty(Playlist.prototype, "avgRating",{ | |
get: function(){ | |
let totalratings = 0; | |
let i = 0; | |
this.tracks.forEach((track)=>{ | |
totalratings += track.rating; | |
i++; | |
}) | |
return totalratings/i ; | |
} | |
}); | |
// create a library | |
let library = new Library('MyCDs','Jellyman'); | |
// create tracks | |
let sting = new Track('Ships','Sting','Dead Mans Boots', 5, 182); | |
let sting2 = new Track('Soul Cages','Sting','Englishman in NY', 4, 132); | |
let jarrett = new Track('Paris','Keith Jarrett','Part 1', 5, 1000); | |
let jarrett2 = new Track('Koln Concerts','Keith Jarrett','Part 2', 3, 560); | |
//create playlists | |
let chill = new Playlist('chill'); | |
chill.addTrackToPlaylist(sting); | |
chill.addTrackToPlaylist(sting2); | |
let jazz = new Playlist('jazz'); | |
jazz.addTrackToPlaylist(jarrett); | |
jazz.addTrackToPlaylist(jarrett2); | |
// add playlists to the library | |
library.addPlaylistToLib(chill); | |
library.addPlaylistToLib(jazz); | |
// out print results | |
console.log(library['name']); | |
console.log(library['playlist']); | |
console.log(chill.name); | |
console.log(sting.name); | |
console.log(chill.duration); | |
console.log(chill.avgRating); |
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
// Author Dave j | |
// These are the rules | |
// Every "a" in the string should be replaced with a "4". | |
// Every "e" in the string should be replaced with a "3". | |
// Every "o" (oh) in the string should be replaced with a "0" (zero). | |
// Every "l" (el) in the string should be replaced with a "1" (one). | |
function obfuscate(word) { | |
var obWord = ""; | |
var mySwitches = { | |
a:'4', | |
e:'3', | |
o:'0', | |
l:'1' | |
}; | |
for (var letter of word) { | |
obWord += mySwitches[letter] || letter; | |
} | |
return obWord | |
}; | |
var obfuscatedWord = obfuscate("abracadabra"); | |
console.log(obfuscatedWord); | |
var obfuscatedWord = obfuscate("passweird"); | |
console.log(obfuscatedWord); | |
var obfuscatedWord = obfuscate("audaciously"); | |
console.log(obfuscatedWord); | |
//this was solved by ... var obWord = word.replace(/a/g, '4').replace(/e/g, '3').replace(/o/g, '0').replace(/l/g, '1'); | |
// return obWord; | |
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 isPalindrome(str) { | |
var noSpaces = str.split(" ").join(""); | |
console.log(str); | |
console.log(noSpaces); | |
var mid = Math.floor(noSpaces.length/2); | |
console.log(mid); | |
var last = noSpaces.length - 1; | |
console.log(last); | |
for (var i = 0; i < mid; i++) { | |
if (str[i] != str[last-i]) { | |
return false; | |
} | |
} | |
} | |
// Test driver code. These should all log true. | |
// console.log(isPalindrome('p') === true); | |
// console.log(isPalindrome('foo') === false); | |
// console.log(isPalindrome('racecar') === true); | |
// console.log(isPalindrome('Kayak') === true); | |
console.log(isPalindrome('a santa at NASA') === true); | |
// console.log(isPalindrome('live without evil') === false); | |
// console.log(isPalindrome('just some random words') === false); | |
// console.log(isPalindrome('kayak') === true); |
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
// Author Dave j | |
// These are the rules | |
// Every "a" in the string should be replaced with a "4". | |
// Every "e" in the string should be replaced with a "3". | |
// Every "o" (oh) in the string should be replaced with a "0" (zero). | |
// Every "l" (el) in the string should be replaced with a "1" (one). | |
function obfuscate(word){ | |
var obWord = word.replace(/a/g, '4').replace(/e/g, '3').replace(/o/g, '0').replace(/l/g, '1'); | |
return obWord | |
}; | |
var obfuscatedWord = obfuscate("abracadabra"); | |
console.log(obfuscatedWord); | |
var obfuscatedWord = obfuscate("password"); | |
console.log(obfuscatedWord); | |
var obfuscatedWord = obfuscate("audaciously"); | |
console.log(obfuscatedWord); |
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 originalWords = process.argv.slice(2); | |
var pigLatinWords = []; | |
for (var i = 0; i < originalWords.length; i++) { | |
pigLatinWords.push(translateToPigLatin(originalWords[i])); | |
} | |
console.log(pigLatinWords.join(' ')); | |
function translateToPigLatin(word) { | |
return word.slice(1, word.length) + word[0] + "ay"; | |
} |
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 input = process.argv[2]; | |
function reverse(original) { | |
return original.split("").reverse().join(""); | |
} | |
if (input){ | |
console.log(reverse(input)); | |
} |
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
//Author DaveJ | |
var salesTaxRates = { | |
AB: 0.05, | |
BC: 0.12, | |
SK: 0.10 | |
}; | |
var companySalesData = [ | |
{ | |
name: "Telus", | |
province: "BC", | |
sales: [ 100, 200, 400 ] | |
}, | |
{ | |
name: "Bombardier", | |
province: "AB", | |
sales: [ 80, 20, 10, 100, 90, 500 ] | |
}, | |
{ | |
name: "Telus", | |
province: "SK", | |
sales: [ 500, 100 ] | |
} | |
]; | |
function getSum(total, num) { | |
return total + num; | |
} | |
for(var i = 0; i < companySalesData.length; i++) { | |
var arrayitem = companySalesData[i]; | |
var tax = 0.10; //tax for SK | |
var arraysales = arrayitem.sales; | |
var reducedarray = arraysales.reduce(getSum) | |
var prov = arrayitem['province']; | |
(arrayitem['province'] === 'BC') ? (tax = 0.12) : (tax = 0.10); | |
(arrayitem['province'] === 'AB') ? (tax = 0.12) : (tax = 0.10); | |
console.log(arrayitem["name"] + ": "); | |
console.log("TotalSales are:" + reducedarray); | |
console.log("TotalTaxes are:" + (reducedarray * tax)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment