The online racing simulator
Javascript Help
(12 posts, started )
Javascript Help
Ey,

Last day(s) i m starting to code a bit with javascript, but i keep on getting the same problem. so i hope somebody can help me with this:

where i am looking for : a fuction that returns true when the filename ends with ".set". ( its for a (team) setup database).

my try:

function getfiletype(Name){
var Typestring='';
var Lastdot =0;
for(i=0; i<Name.length;i++){
if(Name[i] =='.'){
Lastdot = i;
}
}

for(i=Lastdot; i<Name.length; i++){
Typestring += Name[i];
}

if(Typestring == '.set'){
return true;
}
else{
return false;
}
}

when i debug my function its working until the if(Typestring == '.set')
he just totally skips that part. (i cant find out why).

hope somebody can help me with the basics of javascript.
because its kinda new for me

<?php 
function getfiletype(name) {
    return 
name.substr(-4) == ".set";
}
?>

Although to actually get the file extension in JavaScript, have a look at this Stack Overflow question.
Quote from DarkTimes :

<?php 
function getfiletype(name) {
    return 
name.substr(-4) == ".set";
}
?>

substr(-4)

Although to actually get the file extension in JavaScript, have a look at this Stack Overflow question.

many thanks looks way more clean then my code.
Ey i hope you dont mind me ask ,but after google-ing for the last couple of days, i see some people using :

return name.substr(-4) == ".set"

and others:
return name.substr(-4) == '.set'

is there a difference in javascript ? between " " or ' ' ?
As far as I'm aware it's just a preference thing, it makes no difference. I actually tend to use single-quotes ' ', purely because you don't have to hold shift to type them, I'm not sure why I didn't in my example.

Again Stack Overflow has it covered.
okey im getting the hind, i ll use stackoverflow more ^^
Hehe, it wasn't a hint, it's just that Stack Overflow is the single most useful programming resource on the internet.
I second that, StackOverflow is pretty awesome.
Just a little note about DarkTimes' code, which is perfectly fine but it can't handle files whose extension is not 3 chars long (.h .c .torrent .jpeg). I suggest using something like this. (It's not like you need it here, I just thought I should present a "proper" solution in case you need it some day)

function getfiletype(name) {
var typestring = "";
var dot;

for(dot = name.length - 1; dot > -1; dot--) {
if(name.substr(dot, 1) == ".")
break;
}

typestring = name.substr(dot);

return (typestring == ".set");
}

EDIT: Or even better

function getfiletype(name) {
var typestring = name.substr(name.lastIndexOf("."));

return (typestring == ".set");
}


<?php 
function isFileType(fileNamefileType) {
    return (
fileName.substr(fileName.lastIndexOf('.')) == fileType);
}
function 
isSetupFile(fileName) {
    return 
isFileType(fileName'.set');
}
echo ((
isSetupFile(file)) ? 'Is' 'Not') + ' a setup file.';
?>

:bath:
function getFileExtension(filename) {
return filename.split('.').pop();
}

Quote from DarkTimes :
function getFileExtension(filename) {
return filename.split('.').pop();
}


:fence:, you win this round, but there will be others!


<?php 
function isFileType(fileNamefileType) {
    return (
fileName.split('.').pop() == fileType);
}
?>

-
(GeForz) DELETED by GeForz

Javascript Help
(12 posts, started )
FGED GREDG RDFGDR GSFDG