Avatar of Eric Bourland
Eric Bourland
Flag 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
PHPJavaScript

Avatar of undefined
Last Comment
Badotz

8/22/2022 - Mon
Francisco Igor

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

Badotz

One wonders: Homework?
ASKER CERTIFIED SOLUTION
Proculopsis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
leakim971

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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
Badotz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Eric Bourland

ASKER
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

Badotz

Just a FYI: My solution works on 2-dimensional arrays of any length, not just 3.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Eric Bourland

ASKER
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?
Badotz

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 ;-)
_agx_

<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>
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Eric Bourland

ASKER
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
leakim971

you're welcome!!!
Badotz

No worries - glad to help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.