Link to home
Start Free TrialLog in
Avatar of Eric Bourland
Eric BourlandFlag for United States of America

asked on

Assignment: write a new function in JQuery or PHP

I have an assignment in JQuery and PHP and I do not even know how to begin, since I have some acquaintance with, but insufficient experience in, both languages. My assignment is:

Write a new function hardwickday(){ } that takes a nested array of mixed values as its argument (assume that the variable is only nested two levels deep -- an array of arrays).

The output of this function should consist of one line for each of the nested arrays; within each line, the items in each array should be joined together into a long string, with each item separated by the the following string "\n\". Both the root-level array and the nested arrays may be of arbitrary length.

The following input and output should help clarify this function:

INPUT
$myvar = array(
array(7, 'apple', 'fridge'),
array('monkey', 'banana', 3, 5, 9),
array('abcdefghijk')
...etc...
);

OUTPUT (on three lines)
7\n\apple\n\fridge
monkey\n\banana\n\3\n\5\n\9
abcdefghijk

I am supposed to write this function in either JQuery or PHP. Which should I choose -- which is appropriate? I understand the definitions of / concepts of: array, function, and string. But I do not know how to begin this task. Thank you for any ideas.

Eric B
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

if you need to dinamically calculate these values (ant the values are accesible in the current page) without some additional page requests you should use the javascript/Jquery approach, but if you need to process the data after showing the results (ie, generating the array from user inputs) you can use PHP

The question is : where is the location of the source data?
it's generated by some user, extracted from some file or resource?

Depending on how to obtain the source data you'll be more clear about
what approach is the best for you (if the data is in a Javascript array you should use a javascript function)


function getData(datalist){

data="";
for (i=0;i<datalist.lenght;i++){
   for (j=0;j<datalist[i].lenght;j++){
        if (j>0) data+="\\n\\"; 
        data+=datalist[i][j];
       
   }
  data+="\n";
}
return data;
}

testdata=[ ['a','b','c'],['1','2','3'] ];
alert(getData(textdata));

Open in new window

One wonders: Homework?
ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eric Bourland

ASKER

Hello friends,

Sorry to be mysterious: this is not a homework assignment for school; if it were, I would have done the background work and would be more informed. I am definitely not afraid of studying and research.

I am a web developer in Chicago; but, most of my development work has been with ColdFusion and Microsoft SQL Server. I work on a shoestring budget for a small group of not-for-profit organizations in DC and Chicago.

fraigor: I think the data will just come from manually supplied (hardcoded) input, not from a database, for now. I think, for now, it is OK to process the data from a javascript array, using a javascript function.

I really appreciate the helpful replies.

I am studying the javacscript supplied by Proculopsis and leakim971. (leakim971, nice to work with you again.)

I need to get myself educated in javascript and Jquery. In the meantime I will try to not bug you guys too much. =)

I will report back here in a little while.

Thank you all again. Hope your day is going well.

Eric
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The three javascript solutions (from Proculopsis, leakim971, Badotz) work well and make sense, and I am almost ready to close this question.

fraigor, I have a question: would I use the code that sent inside <php> tags, as in the attached code? Thanks for humoring this very basic question.

Yep, gotta do some reading up on PHP and javascript.

Eric
<html>  
<head></head>  
<body>  
<?php  
function getData(datalist){  
  
data="";  
for (i=0;i<datalist.lenght;i++){  
   for (j=0;j<datalist[i].lenght;j++){  
        if (j>0) data+="\\n\\";   
        data+=datalist[i][j];  
         
   }  
  data+="\n";  
}  
return data;  
}  
  
testdata=[ ['a','b','c'],['1','2','3'] ];  
alert(getData(textdata)); 
?>  
</body>  
</html>

Open in new window

Just a FYI: My solution works on 2-dimensional arrays of any length, not just 3.
Badotz;

INPUT  
$myvar = array(  
array(7, 'apple', 'fridge'),  
array('monkey', 'banana', 3, 5, 9),  
array('abcdefghijk')  
...etc...  
);

Open in new window


Yep, that makes sense. =) I am learning a lot.

Question: what does variable xknb do?
I declared all of the vars with a single var statement - the commas at the end act as continuation characters.

xknb is just a means of terminating the var statement; it is not used otherwise. (I used to use "exknob", a nonsense word, but since vowels carry no meaning, I removed 'em ;-)
<ot>
    >> leakim971 @Badotz : check his profile ;-) ...but we never know lol

    ROFL. Knowing him from other zones, I can say Eric's definitely not
    the "do my work/homework for me" type. I only wish my co-worker's on a
    few jobs had his kind of work ethic .. lol
</ot>
Thank you to Proculopsis, leakim971, Badotz for understandable and working solutions.

Shout-out to _agx_ and fraigor. =)

fraigor -- thank you for your time; I think the solution is to use a javascript in this case.

Eric B
you're welcome!!!
No worries - glad to help.