JavaScript from Null: Utility Functions and Debugging
Tutorial Details
- Topic: JavaScript
- Difficulty: Basix
- Format: 24 Minute Video
The Full Series
JavaScript University continues as we develop our first utility function that will allow us to filter and retrieve only the unique values from an array. Along the way, I’ll also teach you how to use the excellent Firebug to debug your code.
Utility Functions and Debugging
Final Code from the Video:
var unique = function(origArr) {
var newArr = [],
origLen = origArr.length,
found,
x, y;
for ( x = 0; x < origLen; x++ ) {
found = undefined;
for ( y = 0; y < newArr.length; y++ ) {
if ( origArr[x] === newArr[y] ) {
found = true;
break;
}
}
if ( !found) newArr.push( origArr[x] );
}
return newArr;
};
// Test it out
var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie'];
myarray = unique(myarray);
console.log(myarray); // jeffrey, allie, patty, damon, zach
Conclusion
So, with this lesson out of the way, you now know how to build your own helpful utility functions. I hope you enjoyed today's video tutorial!
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://laroouse.com esranull
- Yosy
- Yosy
- El garch
- barry
- Dylan
- Adam
- barry
- http://mixmo-anime.blogspot.com kankaro
- Nadeem
- Marcin
- Marcin
- http://ufomuffin.com Alexa
- http://cmstutorials.org krike
- Mike Hopley
- piratelv
- tarek
- Daniel
- Barry
- John
- Barry
- John
- Daniel
- Mike Hopley
- Patrick
- http://jportal.pl shpyo
- Umair
- Myung Ki
- ben
- Ufuk
- Michel
- Timothe Borel
- Mario
