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 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Home: Essex, Current Location: Loughborough
    Posts
    21
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Getting Double Quotes into Coding

    Hi, I have completed the necessary function and it does not need any changes however from the "alert" I need there has to be double quotes surrounding the search 'Lboro'. (I know I may be making a meal of the coding however the lecturer wants us to follow this due to everyone being at different levels of programming).
    I have used the '\' character however the double quotes do not end up in the position I require them?!

    My Coding

    function findAnyU (s){

    var a = s , b , c , d , e = -1;

    for (var i = 0 ; i < pages.length && e == -1 ; i++){
    b = pages[i].indexOf('[');
    c = pages[i].indexOf(']');
    d = pages[i].substring(b+1, c).toLowerCase();
    e = d.indexOf(s.toLowerCase());
    }

    if(e >= 1)
    a += ' found'
    else
    a += ' not found'

    return (a)
    }

    alert (findAnyU('Lboro' , pages));

    Current Alert =

    Lboro found


    Cheers George
    Last edited by georgesofroniou; 11-11-2010 at 04:25 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    \ The backward slash.

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    Home: Essex, Current Location: Loughborough
    Posts
    21
    Thanks
    11
    Thanked 0 Times in 0 Posts
    i have tried the backward slash and when I put it in place it says the search is 'not found'...

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by georgesofroniou View Post
    i have tried the backward slash and when I put it in place it says the search is 'not found'...
    Wrong again! Take no notice of the Doctor. He knows everything about JavaScript from A to B

    Code:
    if(e >= 1) {
    a = '"' + a + '"' + 'found';
    }
    else {
    a = '"' + a + '"' + ' not found';
    }
    "Lboro" found


    “Expert: a man who makes three correct guesses consecutively.” - Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

    The good Doctor has yet to qualify as an expert!
    Last edited by Philip M; 11-11-2010 at 05:40 PM. Reason: Typo

  • Users who have thanked Philip M for this post:

    georgesofroniou (11-11-2010)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,125
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, now, Philip...you *could* use \. If you want to work a bit more. <grin/>

    Heck, you can work harder *and* be more obscure:
    Code:
        return "\"" + a + ( e >= 1 ?  "\" not" : "\"" ) + " found.";
    george: Use Philip's answer.
    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.

  • Users who have thanked Old Pedant for this post:

    georgesofroniou (11-11-2010)

  • #6
    New Coder
    Join Date
    Nov 2010
    Location
    Home: Essex, Current Location: Loughborough
    Posts
    21
    Thanks
    11
    Thanked 0 Times in 0 Posts
    cheers guys, you two are so helpful on this forum!!!

    I ended up working it by myself in my practical lecture, which made me slightly happy
    I did...


    if(e >= 1)
    alert("\"" +a+ "\" found")
    // a += ' found'
    else
    alert("\"" +a+ "\" not found")
    // a += ' not found'

    return (a)
    }
    Last edited by georgesofroniou; 11-11-2010 at 08:28 PM.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    To make it simpler and avoid confusion, you can interchange " and ' as string delimiter so you don't have to use \ to escape the quote literal. This is what Philip did.

  • Users who have thanked glenngv for this post:

    georgesofroniou (11-11-2010)

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Take note that georgesofroniou had previously said he used / and he has edited his post to change that.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,125
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    LOL! Yes, I can see that, now, Dr! And *after* he replied to you, no less.

    No wonder your answer looked...ummm..out of place?
    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.


  •  

    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
    •