-4

I am new to programming and I have a problem for one of my homework and I would like someone who has experience with Javascript or anyone with knowledge of programming to help me to solve this problem please.

I would like to make my function section to return 'true' only if a "filename" ends either in '.jpg' or '.jpeg. Here is the code that I have:

"use strict";

function isWebImageFriendly(filename) {
  return true;
}

I have lots of photos ends with .jpg and.jpeg on my Javascript but I don't know how to write a code so that it only selects the .jpg or .jpeg type of files that I needed.

I was thinking should I write like this:

"use strict";

function isWebImageFriendly(filename) {
<form onsubmit="return isWebImageFriendly(filename);"></form>      
return true;
}
devaaron
  • 25
  • 8
  • 1
    It seems that you want people here to write code for you. That's not how SO works. Please, show some code you've writen so far, and we can try to improve it/show solutions. Have a look here: http://stackoverflow.com/help/how-to-ask – Gerardo Furtado Aug 20 '16 at 09:46
  • 1
    Possible duplicate of [Validate image type using javascript](http://stackoverflow.com/questions/2036158/validate-image-type-using-javascript) – soorapadman Aug 20 '16 at 09:50

1 Answers1

1

You can use regex:

function isWebImageFriendly(filename) {
  return filename.match(/\.jpe?g$/)
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • Thank you for trying to help me out. I tried to use that function that you suggested to me but when i reload my page there are nothing showing my on webpage, is it because I need insert the function under a variable? I have a variable called var photo_details = [ lists of photos....] should I do it like this function isWebImageFriendly(photo_details) {return photo_details.match (/\.jpg?g$/)} – devaaron Aug 20 '16 at 11:38