let's say we have a variable containing a string representing an image and its format like this:
let pictureLabel = 'picture.jpg';
I want a function to remove the format and return the name only. so I tried this but definitely I need a hand as a beginner :)
Here is what I tried without success. Why and how to fix this code:
let pictureLabel = 'picture.jpg';
function withoutFormat(pictureLabel){
const formats = ['png', 'jpg', 'jpeg', 'gif']; // defined formats to be remove
formats.forEach(format => {
console.log(pictureLabel.lastIndexOf(`.${format}`))
if(pictureLabel.lastIndexOf(`.${format}`) != -1){
return pictureLabel.substring(0, pictureLabel.lastIndexOf(`.${format}`));
}
});
}
console.log(withoutFormat(pictureLabel));