...

View Full Version : Getting Double Quotes into Coding



georgesofroniou
11-11-2010, 03:50 PM
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 :)

DrDOS
11-11-2010, 03:53 PM
\ The backward slash.

georgesofroniou
11-11-2010, 05:24 PM
i have tried the backward slash and when I put it in place it says the search is 'not found'...

Philip M
11-11-2010, 06:03 PM
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. :p He knows everything about JavaScript from A to B :D:D



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! :D:D

Old Pedant
11-11-2010, 08:34 PM
Oh, now, Philip...you *could* use \. If you want to work a bit more. <grin/>

Heck, you can work harder *and* be more obscure:


return "\"" + a + ( e >= 1 ? "\" not" : "\"" ) + " found.";

george: Use Philip's answer.

georgesofroniou
11-11-2010, 09:25 PM
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 :D
I did...


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

return (a)
}

glenngv
11-11-2010, 10:25 PM
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.

DrDOS
11-12-2010, 12:05 AM
Take note that georgesofroniou had previously said he used / and he has edited his post to change that.

Old Pedant
11-12-2010, 02:40 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum