Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-17-2012, 03:04 PM   PM User | #1
Michelle.G
New Coder

 
Join Date: May 2012
Location: Ohio
Posts: 17
Thanks: 11
Thanked 0 Times in 0 Posts
Michelle.G has a little shameless behaviour in the past
Exclamation Please help! to make a program with javascript

Hi! frnds, I was wondering if you could help me with the JavaScript programing...

Every possible help will be appreciated

I am very new to the world of programming and JavaScript but I have some Ideas which I would like to execute and I will learn anything and everything in the way to accomplish them...

Program:

user end: the page is about users priorities and displaying back the selected... the program work as a user reaction based comparison module...
where the priorities stored in the program are shown 2 at a time and lets user to select what is more important to him and stores the result in another variable than that variable is shown with another variable which is already stored and the result out of that is stored in another new variable and so on.. in the end it show the result of important selected priorities...

1) say there are 6 variables storing string values:

Code:
 <script type = "text/javascript"> 
 var p1 = "Doctors appointment"; 
 var p2 = "studying for the exam"; 
 var p3 = "Going out for most awaited shopping"; 
 var p4 = "Login on Facebook"; 
 var p5 = "Replying to text msgs"; 
 var p6 = "Go out with friends";
2) there are two sections on the html document:

i) where the variables are displayed dynamically and changes the value on click..
it works like a comparison where user selects (onclick) what is more important to him shows the next value and so on...
in the end stores the result...

functioning(I have no I idea how to do this): say on the html document only two variables are displayed at first and than the user selects one of it as more important to him so the result is stored in another new variable than that new variable is shown with the third stored variable and once one of them is selected it is stored in another new variable and is compared with forth stored variable and so on till the last selected is stored as the result1...

all comparisons must happen on the same div of the html document procedure has to loop three times till 3 results (result1, result2, result3) out of six are selected more important...
result1 has to be removed from the next loop for result 2 as result1 is already selected important by the user..
result1 and result2 are to be removed from the next loop for result 3 as they are already selected important by the user

ii) the second section displays important 3 results (result1, result2, result3) selected by him...


my friend told jQuery can do the dynamic comparison part but I don't know how to use it...

the logic of creating new variable for storing result and then comparing can be improved or changed...

Please help
Michelle.G is offline   Reply With Quote
Old 05-17-2012, 03:19 PM   PM User | #2
Michelle.G
New Coder

 
Join Date: May 2012
Location: Ohio
Posts: 17
Thanks: 11
Thanked 0 Times in 0 Posts
Michelle.G has a little shameless behaviour in the past
Quote:
Originally Posted by iBall View Post
This sounds like a homework assignment.

Post the code you have so far and you are much more likely to get meaningful help.

Also, since you say you are very new, I would suggest you stay away from jQuery for now. After all, jQuery is just a library of normal JavaScript functions. It is not a different language to JavaScript. Therefore, you can't do anything with jQuery that cannot be done with JavaScript.
I am sorry to say but I am out of ideas of creating the user reaction based comparison function part in the program and that is why I am asking help...
I cant think how to write that comparison function

anyways thanks for your reply
Michelle.G is offline   Reply With Quote
Old 05-17-2012, 03:29 PM   PM User | #3
Peeyush
Regular Coder

 
Join Date: Apr 2012
Posts: 104
Thanks: 27
Thanked 2 Times in 2 Posts
Peeyush is an unknown quantity at this point
so do you know any javascript? i mean even a little? have you tried creating something? if you have please post it here. you cant make a program without knowledge of the programming language, copying and pasting scripts from here and there wont help....
__________________
Everything is simpler with jQuery!
Peeyush is offline   Reply With Quote
Old 05-17-2012, 03:41 PM   PM User | #4
Michelle.G
New Coder

 
Join Date: May 2012
Location: Ohio
Posts: 17
Thanks: 11
Thanked 0 Times in 0 Posts
Michelle.G has a little shameless behaviour in the past
Quote:
Originally Posted by Peeyush View Post
so do you know any javascript? i mean even a little? have you tried creating something? if you have please post it here. you cant make a program without knowledge of the programming language, copying and pasting scripts from here and there wont help....
yes I know basics of programing in JavaScript eg: syntax, variable, arrays, data types,operators, built-in objects, DOM, conditions, loops etc...but the program I am trying to create is beyond my logic and that is why I am seeking help in here...

help is always appreciated
Michelle.G is offline   Reply With Quote
Old 05-17-2012, 04:31 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This seems a strange way to go about selecting the first three preferences out of six (if I have understood the assignment correctly). I would suggest using a select list:-

Code:
<html>
<head>
</head>
<body>

<form name='myform'>
<select name = 'list1' id = 'list1' onchange = "showOptions(this)">
<option selected value=""> Choose A Fruit</option>
<option value='Mango'> Mango </option>
<option value='Apple'> Apple </option>
<option value='Orange'> Orange </option>
<option value='Watermelon'> Watermelon </option>
<option value='Plum'> Plum </option>
<option value='Papaya'> Papaya </option>
</select>
<br><br><br>

First Preference:&nbsp &nbsp &nbsp <input type = "text" name = "txt1" id = "txt1" readonly"><br>
Second Preference: <input type = "text" name = "txt2" id = "txt2" readonly"><br>
Third Preference: &nbsp &nbsp  <input type = "text" name = "txt3" id="txt3" readonly"><br>
<br><br>
<input type = "button" name = "but1" value = "Clear Preferences" onclick = "clearPrefs()">

</form>

<script type = "text/javascript">
var chosen = [];
for (var i = 0; i<document.myform.list1.length; i++) {
chosen[i] = 0;
}
var count = 1;

function showOptions(which){
var x = which.value;
var se = which.selectedIndex;
if (chosen[se] == 1) {
alert ("You have alreay chosen that option! Try again!")
return false;
}
if ((x!="") && (count <=3)){
chosen[se] = 1;
document.getElementById("txt"+count).value = x;
count ++;
}
}

function clearPrefs() {
count = 1;
document.myform.list1.selectedIndex = 0;
for (var i=1; i <=3; i++) {
document.getElementById("txt"+i).value = "";
}
for (var i = 0; i<document.myform.list1.length; i++) {
chosen[i] = 0;
}
}

</script>
</body>
</hrml>

There are of course many possible approaches. Someone else may think of a better one.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 05-17-2012 at 04:39 PM.. Reason: improved
Philip M is offline   Reply With Quote
Old 05-17-2012, 05:14 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
here's another way, just for kicks...

Code:
<!doctype html>
<html>
<head>

</head>

<body onload="display()">
<div id="questions">
Please note your preference: <br>
<input type='radio' name='rad' onclick='choose(1)'/><span id="opt1"></span>
<input type='radio' name='rad' onclick='choose(0)'/><span id="opt2"></span>
</div>
<div id="results"></div>

<script type="text/javascript">
var opts=[["Doctors appointment", "studying for the exam"], ["Going out for most awaited shopping", "Login on Facebook"], ["Replying to text msgs", "Go out with friends"]]

var choices=0;

function choose(idx) {
opts[choices].splice(idx,1);
if (choices==2){
document.getElementById("questions").style.display="none"
document.getElementById("results").innerHTML="Your preferences are:<br>"
    for (var i = 0; i < opts.length; i++) {
document.getElementById("results").innerHTML+=opts[i][0]+"<br>"
	}
} else{
choices++
display();
}
}

function display(){
document.getElementById("opt1").innerHTML=opts[choices][0]
document.getElementById("opt2").innerHTML=opts[choices][1]
}


</script>
</body>
</html>
xelawho is offline   Reply With Quote
Old 05-17-2012, 07:41 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The problem there is that the user may prefer "Doctor's appointment" to "Studying for exam", but what if he prefers "Studying for exam" to "Login on Facebook", "Replying to text msgs" or "Go out with friends"?

Or have I misunderstood the assignment?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 05-17-2012, 09:25 PM   PM User | #8
c1lonewolf
Regular Coder

 
Join Date: Sep 2002
Posts: 216
Thanks: 0
Thanked 11 Times in 11 Posts
c1lonewolf is an unknown quantity at this point
If I'm following this correctly, wouldn't it just be easier to create a form with all the options using a checkbox. Then use the box's onchange event handler to adjust an array accordingly.
"Select items according to importance"
Then if you needed to start over you simply uncheck everything.

Just wondering...
__________________
NO Limits!!
c1lonewolf is offline   Reply With Quote
Old 05-17-2012, 09:58 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If it were going to be used in the real world you'd need a way for it to work without JavaScript as well si the obvious starting point would be to simply create all the options as submit buttons and one extra to restart. You'd also have hidden fields to store the value(s) previously selected. The (theoretical) server side code would store the value from the selected submit button in the next unused hidden field when it redisplays the form with that button disabled until all are filled after which it would then proceed to process them all.

The JavaScript would then be attached to this si that it provides equivalent functionality by storing the value of the submit button in the next empty hidden field, disabling that submit button and then aborting the submit. It would do whatever is necessary as additional processing after the last hidden field is filled inb order to process all the hidden fields.

By using that design for the JavaScript you'd have a version that could be made to function without JavaScript by adding server side processing without having to make any changes to the form in order to implement it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 05-17-2012, 10:18 PM   PM User | #10
Michelle.G
New Coder

 
Join Date: May 2012
Location: Ohio
Posts: 17
Thanks: 11
Thanked 0 Times in 0 Posts
Michelle.G has a little shameless behaviour in the past
Hey sorry for the late reply had logged off by the time you all replied...

Thank you all for your efforts and contribution especially xelawho and Philip M...

I feel I was not efficient to explain the module correctly the closest was xelawho...

let explain with an example:

say there are 6 variables storing string values:

var p1 = "Doctors appointment";
var p2 = "studying for the exam";
var p3 = "Going out for most awaited shopping";
var p4 = "Login on Facebook";
var p5 = "Replying to text msgs";
var p6 = "Going out with friends";

how it works on the users end:
(this is what user sees on the page)

What is more important to you? if you had to do these thing at the same time, which one would you give the priority?
Options (click any one)

1) Doctors appointment // "Login on Facebook" // "Replying to text msgs" // "Going out with friends"

or

2) studying for the exam // "Going out for most awaited shopping"


//First click (select): if the user click on option 1 then option 1 remains there and option 2 changes to var p3 ("Going out for most awaited shopping")

//Second click (select): if the user then selects the option 2 where the value is var p3 "Going out for most awaited shopping" it remains unchanged there and the option 1 having value p1 ("Doctors appointment") changes to p4 ("Login on Facebook")

//Third click (select): if the user then selects again option 2 where its value is still var p3 then option 1 changes from var p4 ("Login on Facebook") to var p5 ("Replying to text msgs")

//Fourth click (select): if the user still selects option 2 where its value is still var p3 then option 1 again changes from var p5 to var p6 ("Go out with friends")

//Fifth click (select): if the user selects option 1 where its value is var p6 ("Going out with friends") over option 2 var p3 ("Going out for most awaited shopping")...
Then the result is stored as result1 and is displayed on the screen and the next loop starts for result2 where result1 (in example:"Going out with friends") has to be removed from the next loop for result 2 as result1 is already selected important by the user..
result1 and result2 are to be removed from the next loop for result 3 as they are already selected important by the user...


Yes! it is wierd type of selection and is complex but that is how it has to be

I hope I was efficient in explaining the module correctly this time

awaiting your reply

Last edited by Michelle.G; 05-17-2012 at 11:17 PM..
Michelle.G is offline   Reply With Quote
Old 05-17-2012, 11:00 PM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I think what is confusing is that you keep showing things like this:
Quote:
1) Doctors appointment // "Login on Facebook" // "Replying to text msgs" // "Going out with friends"
or
2) studying for the exam // "Going out for most awaited shopping"
But *I THINK* what you mean is that what is DISPLAYED is only
Quote:
1) Doctors appointment
or
2) studying for the exam
And you were just using the // stuff to show the parts that are HIDDEN to the user.

********

Am I right?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-17-2012, 11:16 PM   PM User | #12
Michelle.G
New Coder

 
Join Date: May 2012
Location: Ohio
Posts: 17
Thanks: 11
Thanked 0 Times in 0 Posts
Michelle.G has a little shameless behaviour in the past
Quote:
Originally Posted by Old Pedant View Post
I think what is confusing is that you keep showing things like this:

But *I THINK* what you mean is that what is DISPLAYED is only

And you were just using the // stuff to show the parts that are HIDDEN to the user.

********

Am I right?
yes true!! // means hidden... but the hidden part corresponds to the explanation given below...

Please see the explanation below to understand the functioning
Michelle.G is offline   Reply With Quote
Old 05-18-2012, 12:07 AM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, so the basic idea is simple. BUT... But what happens when you run out of one set of choices?
Quote:
1) Doctors appointment // "Login on Facebook" // "Replying to text msgs" // "Going out with friends"
2) studying for the exam // "Going out for most awaited shopping"
Suppose the user choose option 1 twice.
Code:
Start:
1) Doctors appointment
2) studying for the exam 

Choose option 1:
1) Doctors appointment 
2) Going out for most awaited shopping

Choose option 1:
1) Doctors appointment 
2)
So then, at this point, you show "Doctors appointment" in the output area and start over with
Code:
1) "Login on Facebook" // "Replying to text msgs" // "Going out with friends"
2) studying for the exam // "Going out for most awaited shopping"
Is that right?

So you do that until what?

And what happens if the FIRST TWO end results are
Code:
studying for the exam
Going out for most awaited shopping
Now there are no choices left in the second list.

Is the game over?

*********

You simply have *NOT* explained thoroughly enough and/or considered/explained all possible scenarios.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-18-2012, 12:15 AM   PM User | #14
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
going for a wild swing here...

in that scenario, Doctors appointment gets saved as favorite 1 and the cycle of questions starts again, but with Doctor's appointment removed from the list

and so on until you have 3 faves?
xelawho is offline   Reply With Quote
Old 05-18-2012, 12:37 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Let me make that real problem scenario clearer:

These are the two sets of choices:
Quote:
1) Doctors appointment // "Login on Facebook" // "Replying to text msgs" // "Going out with friends"
2) studying for the exam // "Going out for most awaited shopping"
So we start with:
Code:
1) Doctors appointment
2) studying for the exam
The user chooses option (2)

Which then gives us
Code:
1) Login on Facebook
2) studying for the exam
The user choose option 2 twice more and we have
Code:
1)
2) studying for the exam
So we put "studying for the exam" into the favorites list and now we start over with
Code:
1) Doctors appointment
2) Going out for most awaited shopping
Again, the user choose option 2 four times in a row and you end up with
Code:
1)
2) Going out for most awaited shopping
So you put "Going out for most awaited shopping" on the favorites list and you start again with
Code:
1) Doctors appointment
2)
OOPS! We ran out of items in the option (2) list!

But the user REALLY wants to choose "Replying to text msgs" as his/her third favorite.

BUT THERE IS NO WAY TO DO SO!

**********

The whole thing seems BADLY BROKEN to me. Unless the two sets of options *EACH* have AT LEAST THREE choices, there is no way to end up with three favorites each and every time.

**********

And by the way: Is this assignment REALLY being given in OHIO?

I am skeptical. I can't imagine any American teen-ager using the phrase "Going out for most awaited shopping". That sounds like a literal translation of Chinese or some other language.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.