Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Simple code not working. Have looked through it everything looks right to me.

    var goodResponses = ["good", "Good", "great", "Great"];
    var response = prompt("Hello. What is your name?");
    response = prompt("Nice to meet you, " + response + ". How're you doin?");
    var bad = true;
    for(var i = 0, i < goodResponses.length, i++) {
    if(response == goodResponses[i]) {
    alert(“I’m glad to hear that. Anyway, I ought to get going. Bye.”);
    bad = false;
    }
    }
    if(bad) {prompt(“That’s too bad. Anyway, try to have a good day. I’ll see you later.”);}

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,908
    Thanks
    36
    Thanked 1,057 Times in 1,053 Posts
    First: Does this look familiar?
    for(var i = 0; i < goodResponses.length; i++) { Not commas

    and why the promp for the last line?
    Maybe this is better
    Code:
    <script>
    var goodResponses = ["good", "Good", "great", "Great"];
    var response = prompt("Hello. What is your name?");
    response = prompt("Nice to meet you, " + response + ". How're you doin?");
    var bad = true;
    
    for(var i = 0; i < goodResponses.length; i++) {
    	if(response == goodResponses[i]) {
    		alert("I'm glad to hear that. Anyway, I ought to get going. Bye.");
    		bad = false;
    	} 
    }
    if(bad) alert("That's too bad. Anyway, try to have a good day. I’ll see you later."); 
    </script>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. Users who have thanked sunfighter for this post:

    ed2278 (Jan 10th, 2019)

  4. #3
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you. I thought JavaScript used , and C C++ and C# used ; apparently I was wrong.

  5. #4
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am sorry. I am now having more problems when trying to add more funcitonality.

    var goodResponses = ["good", "great", “fantastic”, “fine”, “i am doing great”, “i am doing great.”, “i am doing fine”, “i am doing fine.”];
    var response = prompt("Hello. What is your name?");
    response = prompt("Nice to meet you, " + response + ". How're you doin?");
    var bad = true;
    for(var i = 0; i < goodResponses.length; i++) {
    if(response == goodResponses[i]) {
    alert(“I’m glad to hear that. Anyway, I ought to get going. Bye.”);
    bad = false;
    }
    }
    if(bad) alert(“That’s too bad. Anyway, try to have a good day. I’ll see you later.”);

  6. #5
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,908
    Thanks
    36
    Thanked 1,057 Times in 1,053 Posts
    Everytime I copy and paste your code that you post here - it don't work. If I change the quotes it does. From your post = “fantastic” Notice the angle on the quotes. They should be straight like this ". Don't know what your text editor is but I'd get a new one. Down load and try notepad++ or sublime or VSCode. See which one you like. My choice is VSCode.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  7. #6
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Understood. Thanks.

  8. #7
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,269
    Thanks
    4
    Thanked 476 Times in 464 Posts
    Quote Originally Posted by ed2278 View Post
    Thank you. I thought JavaScript used , and C C++ and C# used ; apparently I was wrong.
    They actually use both for the same things, but most people don't realize you can use comma's in the first part of a for loop.

    for example:

    Code:
    var allLI = document.getElementsByTagName('li');
    
    for (var i = 0, result = '', li; li = allLi[i]; i++) {
      result += li.textContent + ' ';
    }
    That may be where you saw comma's. You can declare multiple variables in any var declaration in JavaScript by comma delimiting them. You still use semi-colons to separate the condition and the iteration from the var. In most flavors of C you can do the same thing with 'int', 'double', or any other variable type declaration, though it's uncommon for programmers to use it as they're either unaware of it, or find it 'unclear'.

    Good rule of thumb? JavaScript is a "C Syntax language" so 90% of the syntax you'd expect from C works in JS. It's why I often joke that JavaScript and PHP are just C with Mr. Winkie tucked back, in a slinky dress, high heeled boots, and theatrical makeup all tarted up for a night on the town.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  9. Users who have thanked deathshadow for this post:

    ed2278 (Jan 12th, 2019)


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •