Jump to content

Search the Community

Showing results for tags 'for loop'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • HTML/XHTML
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • XSLT/XSL-FO
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Languages

Found 12 results

  1. Im trying to put a stored value inside the range of a "for" loop, and I don't know if that's permitted, an example would be: M = np.array([[8,8,8,8], [8,8,8,8], [8,8,8,8], [8,8,8,8]]) for j in range (4): for i in range(4): for k in range(2,M[i,j]): if M[i,j] % k == 0: print("yes") else print("no")
  2. Hello, to my experience the following does not work (tryit-editor w3School), but it does with a "while" loop instead. Are "for" loops not allowed within classes? <p id="demo">result</p> class Test{ counter(){ var result=0; for(i=0;i<10;i++){ result++; } return result; } counter2(){ var result=0; while(result<10){ result++; } return result; } } var obj=new Test(); document.getElementById("demo").innerHTML=obj.counter(); //document.getElementById("demo").innerHTML=obj.counter2();
  3. Hello folks. I am currently learning Python programming. I have created a fill in the blanks quiz game. In this game, the user has to select a level hard, medium, easy. On the basis of teh level selected, the respective question and its answers are passed as parameters into the function, question validation, this function takes user input and sees whether is matches the question and answer input. If it does, then the answer is passed onto the function, traverse. At traverse, the blanks are filled in and the sentence is returned with missing blank filled in. This seems to work well upto filling in the fourth blank. At this stage if I get the answer wrong on the first attempt and I later answer correctly, it then prompts me to fill in the 4th again. Its acting like an infinite loop. I cant for the life of me seem to solve this!!!! The problem lies here: time.sleep(2) print "wrong answer you have", tries, " attempts left" while tries>0: print "Fill in ",blanks[index], " please" response = raw_input() response = response.upper() if response == answers[index]: time.sleep(2) print "congratulations!!! " traverse(question, answers, index) else: tries = tries - 1 print "wrong answer you have", tries, "attempts left" print "Arividechi" exit() codeaquiz.py
  4. I'm baffled by this - I am trying to master for loops and I seem to have run into a snag. I looked up w3's nested for loops and got an example ( you can find it @ javascript statements and click on the 'for' link - at the bottom of the page) and it works great and I understand it . Then I tried to inject an 'if' statement to interrupt it on a condition, but I got some unexpected results and I can't see what is wrong. The logic seems correct - maybe someone can help me. The 1st listing is directly from the example and in the second listing I injected a 2 line if statement - I got the results I=o, j = 2 993 994 and there it stopped. I also changed the values of i & j, but I don't see why that matters. Thanks for any help! <!DOCTYPE html> <html> <body> <p>In this example, we have two loops. Because there is one loop that is inside the other, we call this a nested loop. The first loop is often called an "outer loop", and the second is often called the "inner loop" because it is inside the first, outer loop.</p> <p>The outer loop executes first, and for each time the outer loop is executed, the inner loop will also execute.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i, j; for (i = 0; i < 3; i++) { text += "<br>" + "i = " + i + ", j = "; for (j = 10; j < 15; j++) { document.getElementById("demo").innerHTML = text += j + " "; } } } </script> </body> </html> <!DOCTYPE html> <html> <body> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var text = ""; var i, j; for (i = 0; i < 5; i++) { text += "<br>" + "i = " + i + ", j = "; for (j = 2; j < 5; j++) { document.getElementById("demo").innerHTML = text += j + " "; if (i = j) { text += 99; } } } } </script> </body> </html>
  5. I need to create variable names in a loop (part of this seems to be working) and then get 26 of the 39 variables to perform simple math (for example: variable -= 10). Getting the variables recognized as variables, and not text, seems to be the issue. This works properly, it accesses 13 canvases named canvas1 through canvas13: for (i = 1; i < 14; i++) { c = document.getElementById("canvas" + i.toString()); context = c.getContext("2d"); ... } However, when I try to dynamically create canvas names, and dynamically create 2 variables in the same line, the variables are created as text. for (i = 1; i < 14; i++) { document.getElementById('canvas' + i).style.filter = "brightness((bright" + i + " -= 10)%) contrast((cont" + i + " -= 10)%)"; } Probably a dozen variations on this theme have failed. In a nutshell, the variables bright1 and cont1 through bright13 and cont13 are not recognized as variables that can be manipulated with math, they're recognized as text. Right now I'm typing out 13 lines of code: document.getElementById('canvas1').style.filter = "brightness(" + (bright1 -= 10) + "%) contrast(" + (cont1 -= 10) + "%)"; ... document.getElementById('canvas13').style.filter = "brightness(" + (bright13 -= 10) + "%) contrast(" + (cont13 -= 10) + "%)"; which is inefficient, to say the least. So, what is the proper syntax to use so that bright1 through bright13 and cont1 through cont13 are variables instead of text? Thank you!
  6. I do not get the input value x to add to i in the for loop. What am I doing wrong? <!DOCTYPE html> <html> <body> <input id="inp" type="number" > </input> <button type="button" onclick=myFunction() > click</button> <p id="result" > </p> <script> function myFunction(){ var txt= ""; res = document.getElementById("result"); res.innerHTML=""; var x= document.getElementById("inp").value; var i= 0; for ( ; i < 20; i++) { txt += "input" + i + "<br>"; i = i += x ; } res.innerHTML=txt; console.log(txt += "input is " + (i+x) + "<br>"); console.log(i); } </script> </body> </html>
  7. Greetings! I do not understand why I have to describe that variable " ". http://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex When I put a letter in it, it returns "aThe number is 0 (...)" only the first character, not every loop. So why we need to describe empty value. Why we want to write there nothing ->
  8. j.silver

    Vague Notice

    Hi, This block code is from W3schools tutorials: <?php $cars = array("Volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x <= $arrlength; $x++) { echo $cars[$x]; echo "<br>"; } ?> The original block was without the equal sign in $x <= $arrlength; I have added the equal sign to see what would happen. I got the same result but followed with: Notice: Undefined offset: 3 in C:\xampp\htdocs\Testing\testing.php on line 14 I would appreciate it if someone would explain why I am getting this notice and its meaning. I thought the loop will stop and give the same result without any notice when it hits no value for the equal sign.
  9. Hello, Can anyone tell me how I create a for loop which incurs a sentence of 10 pixels to 100 pixels? So the word sentence font-size from 10 to 100 px. for ($x = "sentence"; $x "font-size" ; $xxx) { echo "$x <br>";} thanks in advance.
  10. Hi, I just started to study javascript and I came across in one of your page to the following notation: var text=""; The page is from w3school tutorial: http://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex I do not understand what it means and why should be used in the loop of the above example. I tried to define var text; in its place but the result is: undefinedThe number is 0The number is 1The number is 2The number is 3The number is 4 The only difference is the world 'undefined' at the beginning of the first line. Many thanks in advance for your help
  11. In the For Loop tutorial of w3si did'nt understand some code line of the following js code, i indicated it below ------------------------------------------------------------------------------------------------------------------------<button onclick="myFunction()">Try it</button><p id="demo"></p> <script>function myFunction(){var x="",i; <---[ what this type of var means, and wat this "" means, ]for (i=0;i<5;i++) { x=x + "The number is " + i + "<br>"; <---[ What the x=x means] }document.getElementById("demo").innerHTML=x;}</script>------------------------------------------------------------------------------------------------------------------------Actually i tried and found it can be write another method, but why w3s written the above indicated method, and not mentioned anywhere about this kind of for loop method.
  12. Hi All,I am a newbe in javascript but curious to learn javascript. I have made a table using divs by javascript for loop and want to add diffrent content in diffrent divs. for example: first div should contain an image, second one should contain an paragraph, third one should contain a button element etc, but I don't know how this to be done.so please anyone help me to make me aware of this idea. here is my code. <style type="text/css">.parent { width: 1010px; height: auto; border: 1px solid red; margin: 0 auto; clear: both; display: table;}.child{ width: 200px; height: 100px; border: 1px solid blue; text-align: center; display: table-cell;}</style><script type="text/javascript">document.write('<div class="parent">');var x = 0;while (x<10) {document.write('<div class="child"></div>');x++;}document.write('</div>');</script> thanks in advance.
×
×
  • Create New...