7 Replies - 504 Views - Last Post: 21 February 2014 - 04:39 AM

#1 psychocodes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 14

noob having a problem with loop... any suggestions?

Posted 19 February 2014 - 12:28 AM

Hello... I am in learning stage.. now a days i am learning javascript..
i have tried loop... but its not working correctly....
 <script>

var table=prompt("enter table number");// if i put 2 here
var s=prompt("start from");    //and 1 here
var c=prompt("end on");    //and 10 here... it will work..... but if s=2 and c=10 it is not working...?
var b=c;
var a=s;

for(a; a<=c; a++){
	
	document.write(table + "x" + a+ "=" +(table*a)+"<br>");
	} </script> 


on first prompt put the value 2 ,
on the second prompt put 1,
on the third prompt put 10,
or
on first prompt put the value 2 ,
on the second prompt put 22,
on the third prompt put 30,


then it will work....
but
on first prompt put the value 2 ,
on the second prompt put 2,
on the third prompt put 10,
then its not working??
( if a am putting values manually in coding then its working perfectly..)



what is the mistake/problem?
what can i do to make it work on that values.....

and Sorry for my bad english....

Is This A Good Question/Topic? 0
  • +

Replies To: noob having a problem with loop... any suggestions?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: noob having a problem with loop... any suggestions?

Posted 19 February 2014 - 02:17 AM

Quote

then its not working??

and that means what exactly?


Quote

what is the mistake/problem?

I see 3 problems here. (some of them typical for beginner tutorials)

  • never ever use document.write(). really! use forms instead
  • donít use propmpt(). use form inputs instead
  • in Java≠Script there are data types (String, Number, Object, etc.). depending on the data type of a variable, behaviour can differ (esp. with the + operator). e.g. all your variables received from prompt() are ... Strings!


unfortunately to solve problem 1 & 2 you need to use HTML and Java≠Scriptís HTML interface. so letís set up some code for that:
<!DOCTYPE html>
<html>
  <head>
    <title>Test script</title>
  </head>
  <body>
    <div><!-- one line per form element -->
      <label for="factor">Enter table number</label>
      <input id="factor" type="number">
    </div>
    <div>
      <label for="start">start from</label>
      <input id="start" type="number">
    </div>
    <div>
      <label for="end">end at</label>
      <input id="end" type="number">
    </div>
    <div>
      <button id="run" type="button">Go</button>
    </div>
    <div id="result"></div>
  </body>
</html>


so far we have a functional user interface so you do not need to reload the page each time you want to run the script.

next is the script itself.

first we use a function for the output itself. (running on the principle Input-Processing-Output)
// remember to use meaningful variable names,
// JS doesnít care about some letters more or less
function makeLine(multiplier, sequenceNumber)
{
    return "<p>"+multiplier+" ◊ "+sequenceNumber+" = "+multiplier*sequenceNumber+"</p>";
}


and a function for the loop
function makeMultiplicationTable(from, to, multiplier)
{
    // init output variable
    var output = "";
    // loop
    for (var i = from; i <= to; i++) {
        // add each line to the output variable
        output += makeLine(multiplier, i);
    }
    return output;
}


now the most complicated part, talking to the HTML
// we want the action to happen on the button click
// so we have to register an event (click) handler
// again, observe the Input-Processing-Output principle
document.getElementById("run").addEventListener("click", function(evt) {
    // get the input
    // document.getElementById() gets the input element
    // .value gets the input elementís number
    // the + in front of that converts the received text into a number
    var multiplier = +document.getElementById("multiplier").value;
    var start      = +document.getElementById("start").value;
    var end        = +document.getElementById("end").value;
    // get a reference to the output element
    var display    =  document.getElementById("result");

    // run the code
    var output = makeMultiplicationTable(start, end, multiplier);

    // push the result back into HTML
    display.innerHTML = output;
});


Iím sure there will be question about that code, but first try to understand what each line of the code is doing.
Was This Post Helpful? 1
  • +
  • -

#3 psychocodes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 14

Re: noob having a problem with loop... any suggestions?

Posted 19 February 2014 - 03:40 AM

<Thanks> src=" Thanks Dormilich... there is alot of thing for me to learn.. it helped me alot.....i run it" />


Posted Image

it took me nearly 30 minutes to understand..
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: noob having a problem with loop... any suggestions?

Posted 19 February 2014 - 05:27 AM

Quote

it took me nearly 30 minutes to understand..

currently I am on a Node.js login tutorial. itís the third day and Iíve implemented part 1 of 3 ...
Was This Post Helpful? 0
  • +
  • -

#5 psychocodes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 14

Re: noob having a problem with loop... any suggestions?

Posted 21 February 2014 - 03:35 AM

[quote name='Dormilich' date='19 February 2014 - 05:27 AM' timestamp='1392812827' post='1972394']

Quote

currently I am on a Node.js login tutorial. itís the third day and Iíve implemented part 1 of 3 ...


where can i watch it? I would love to watch the tutorial..
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: noob having a problem with loop... any suggestions?

Posted 21 February 2014 - 03:42 AM

http://scotch.io/tut...setup-and-local

the first part wasnít that difficult, but when the third-party dependencies came up it got a bit difficult (esp. if you have neither of those social accounts).
Was This Post Helpful? 1
  • +
  • -

#7 psychocodes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 14

Re: noob having a problem with loop... any suggestions?

Posted 21 February 2014 - 04:25 AM

it is very difficult for me because i am a new student. i'got only 6 hours of classes of javascript from my institute..

This post has been edited by Dormilich: 21 February 2014 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: noob having a problem with loop... any suggestions?

Posted 21 February 2014 - 04:39 AM

yupp, that is advanced Java≠Script coding.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1