Click to See Complete Forum and Search --> : Efficiency


jaystead
07-27-2003, 10:33 PM
I have never written a script, we'll get that out of the way now... ;)

I have downloaded a script and made some modifications to suit my needs. I am wondering, as I am an amateur, if there is a more efficient way of accomplishing this. This script works and does everything I need it to do, but I am just curious.

Its purpose is to randomly select a song, embed it in the page and display the song title:



<!--
//By George Chiang (http://www.abstract.simplenet.com) More JavaScripts here!
//Modified by Jay Stead (http://www.stead.ca)
var title
var sound1="/music/PavementCracks.asf"
var sound1_title="Annie Lennox - Pavement Cracks"
var sound2="/music/nottheone.asf"
var sound2_title="Daniel Bedingfield - If You're Not The One"
var sound3="/music/onahigh.asf"
var sound3_title="Duncan Sheik - On a High"
var sound4="/music/eachday.asf"
var sound4_title="Ronan Keating - Lovin' Each Day"
var sound5="/music/boysofsummer.asf"
var sound5_title="DJ Sammy - The Boys of Summer"
var x=Math.round(Math.random()*4)
if (x==0) x=sound1
else if (x==1) x=sound2
else if (x==2) x=sound3
else if (x==3) x=sound4
else x=sound5
document.write('<embed src="'+x+'"hidden="true" autostart="true" nosave="true">')

if (x == sound1) document.write('<small class="min">You are listening to: '+sound1_title+'</small>')
else if (x == sound2) document.write('<small class="min">You are listening to: '+sound2_title+'</small>')
else if (x == sound3) document.write('<small class="min">You are listening to: '+sound3_title+'</small>')
else if (x == sound4) document.write('<small class="min">You are listening to: '+sound4_title+'</small>')
else if (x == sound5) document.write('<small class="min">You are listening to: '+sound5_title+'</small>')
//-->


Any help appreciated!
Jay

xataku_nakusute
07-28-2003, 12:38 AM
by "efficient", what exactly are you implying?

Khalid Ali
07-28-2003, 12:53 AM
if u used array , you can reduce the code t almost half th esize,and hence the improvement in efficiency as well,however in this script you may not even notice much of difference in efficiency,you can definitely reduce the number of lines of code to half....

Charles
07-28-2003, 01:42 AM
<script type="text/javascript">
<!--
Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}

var sound = new Array()
sound[0] = {href:'/music/PavementCracks.asf', title:'Annie Lennox - Pavement Cracks'}
sound[1] = {href:'/music/nottheone.asf', title:'Daniel Bedingfield - If You\'re Not The One'}
sound[2] = {href:'/music/onahigh.asf', title:'Duncan Sheik - On a High'}
sound[3] = {href:'/music/eachday.asf', title:'Ronan Keating - Lovin\' Each Day'}
sound[4] = {href:'/music/boysofsummer.asf', title:'DJ Sammy - The Boys of Summer'}

var s = sound.random();
document.write('<embed src="', s.href, '"hidden="true" autostart="true" nosave="true">')
document.write('<small class="min">You are listening to: ', s.title, '</small>')
// -->
</script>

jaystead
07-28-2003, 10:31 AM
Well that is definitely much more efficient! :D

I only had to change one thing. A lot of my song titles have apostrophes (') in them which caused errors in the script, so I change the quotations from ' to " which seemed to work. Now I can have apostrophes (') in my song titles.

Thank you so much!
Jay

steelersfan88
07-28-2004, 01:46 PM
if you have ' in your song title, you could either change the quotes, or escape the apostrophe (\')