Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active January 3, 2016 23:09
Show Gist options
  • Save Chryus/8532688 to your computer and use it in GitHub Desktop.
Save Chryus/8532688 to your computer and use it in GitHub Desktop.
beginner functions in JavaScript
//this function finds the absolute value of a number
function absolute(num) {
if (num < 0) {
return -num;
} else {
return num;
}
}
console.log(absolute(-10));
Extra function:
//this function iterates over every element in the array heaven, searches for elements that start with the letter b, and returns a string with all those elements.
var heaven = [34, [], "bodacious", "barbecues", "begin", "whatever", "by", "battering", true, "bacon"];
var bStuff = "";
for (var i = 0; i < heaven.length; i++) {
if (heaven[i][0] == "b") {
bStuff += heaven[i] + " ";
}
}
console.log(bStuff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment