Last active
January 3, 2016 23:09
-
-
Save Chryus/8532688 to your computer and use it in GitHub Desktop.
beginner functions in JavaScript
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
//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