Wednesday, 15 February 2012

javascript - Prompt for array and counter -



javascript - Prompt for array and counter -

hello need help web scripting homework: function: counter parameter: array of numbers. returns: numbers of negative, zeros, , positive numbers in array note: must utilize switch statement in function. have far

<!doctype html> <head> <title>exercise 4.11</title> </head> <body> <script> var num = new array(); num[0] = parseint(prompt("please come in number",""),10); num[1] = parseint(prompt("please come in number",""),10); num[2] = parseint(prompt("please come in number",""),10); num[3] = parseint(prompt("please come in number",""),10); num[4] = parseint(prompt("please come in number",""),10); counter(num); function counter(num) { var i, count = { negative: 0, zero: 0, positive: 0, }; for(i = 0; < num.length; i++) { switch (true) { case (num[i] < 0): count.negative++; break; case (num[i] === 0): count.zero++; break; case (num[i] > 0): count.positive++; break; } } document.write(count); }; </script> </body> </html>

there several things wrong this, you've not asked question i'll address them @ random.

you've created method called counter task of processing array, you've not called it you're attempting document.write after you've exited method calling return first. your prompts homecoming strings, need parsed numbers using parseint

a quick nudge in right direction:

call counter method after you've prompted numbers, , alter syntax in method declaration can phone call before it's defined:

var num = new array(); num[0] = prompt("please come in number",""); num[1] = prompt("please come in number",""); num[2] = prompt("please come in number",""); num[3] = prompt("please come in number",""); num[4] = prompt("please come in number",""); counter(num); function counter(num){ // logic }

update

at end of method, you'll want print results. you've discovered, javascript trying describe object you've asked print converting string. string ends giving pretty uninsightful. improve print each individual key in count object:

document.write(count.negative); document.write(count.zero); document.write(count.positive);

i using other document.write, reasons go beyond scope of question.

javascript html

No comments:

Post a Comment