Tuesday 15 June 2010

javascript - JS: build function and multidim array; then build loop to display array contents in sentence form and prompt user for values where content is missing -



javascript - JS: build function and multidim array; then build loop to display array contents in sentence form and prompt user for values where content is missing -

i newbie javascript , grateful guidance in accomplishing next task (which has 3 parts) maintain getting stuck. reading through other questions , answers on site, i'm thinking there may more 1 way of doing it. larn most efficient , valid approach take be.

thanks much steering me in right direction (and beingness patient egregious beginner's mistakes , misconceptions!)

the directions:

a) build function “ipaid” accepts 3 parameters: month, utility , amount. outputs “on [month], paid $[amount] [utility].” using console.log function. utilities paid each month include water, gas, , electricity.

b) build 2 dimensional array contains row month, , column water , gas. set amounts paid in corresponding utilities. (i know, left electricity out).

c) build loop on months. phone call ipaid display paid, using content in arrays. each electricity bill, utilize window.prompt function amount paid user.

what i've got far: (a whole lotta nuthin'...) :-(

a) correct? or way off base?

ipaid = function(month, utility, amount) { this.month = month; this.utility = utility; this.amount = amount; console.log("in " + this.month + " paid $ " + this.amount + " " + this.utility + "."); };

perhaps need like:

ipaid = function(month, utility, amount) { this.month = month; this.utility = utility; this.amount = amount; console.log("in " + month[i] + " paid $ " + amount[i][j] + " " + utility[j] + "."); };

b) how go building two-dimensional array? , need leave room "electricity?

like this?

var mybills = new array(12,2); mybills[0][0] = new ipaid("january","water",45); mybills[0][1] = new ipaid("january","gas",15);

or should define month, , utility separately it's more this? (i don't think syntax correct, though)

var mybills = new array(12,2); mybills[0][0] = (month[0],[utility[0],45); mybills[0][1] =

c) close right way prompt? or should define electricity utility[2]? need utilize "push" add together array?

for (var = 0; < month.length; i++) { var electricity = prompt("what did pay electricity in " + month[i] + "?", ""); return(electricity);

this should started:

var months = [ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" ]; var utilities = [ "electricity", "water" ]; var ipaid = function(month, utility, amount) { this.month = month; this.utility = utility; this.amount = amount; } var mybills = []; (var month = 0; month < months.length; month++) { (var utility = 0; utility < utilities.length; utility++) { if (!(mybills[month] instanceof array)) { mybills[month] = []; } var amount = 10; // fill propt or whatever. mybills[month][utility] = new ipaid(months[month], utilities[utility], amount); } } (var month = 0; month < mybills.length; month++) { (var utility = 0; utility < mybills[month].length; utility++) { console.log("in " + months[month] + " paid $" + mybills[month][utility] + " " + utilities[utility] + "."); } }

link codepen

i don't think new array(12,2) doing expected. in javascript typically allocate arrays iterating through , assigning values. 2 dimensional array requires nested loop.

javascript

No comments:

Post a Comment