|
| 1 | +# 0005 isJavaScriptFile ( L-B ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Write a function that returns an new array of true and false values based on the input array. The function should return true if the corresponding element is a JavaScript file. |
| 6 | + |
| 7 | +## Test Cases |
| 8 | + |
| 9 | +- isJavaScriptFile(['L-B.js', 'L-B.css', 'L-B.html', 'L-B.js']); // [true, false, false, true] |
| 10 | +- isJavaScriptFile(['L-B.js', 'L-B.js', 'L-B.js', 'L-B.js']); // [true, true, true, true] |
| 11 | +- isJavaScriptFile(['L-B.css', 'L-B.css', 'L-B.css', 'L-B.css']); // [false, false, false, false] |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +```javascript |
| 16 | +function isJavaScript(file) { |
| 17 | + let trueName = []; |
| 18 | + for (const ele of file) { |
| 19 | + if (ele.endsWith(".js")){ |
| 20 | + trueName.push(true) |
| 21 | + } |
| 22 | + else{ |
| 23 | + trueName.push(false) |
| 24 | + } |
| 25 | + } |
| 26 | + return trueName; |
| 27 | +} |
| 28 | + |
| 29 | +const fileInputName = ["javascriptProject.js", "app.js","js.png","clock.java", "image.js"]; |
| 30 | +const fileName = isJavaScript(fileInputName); |
| 31 | +``` |
| 32 | + |
| 33 | +## How it works |
| 34 | + |
| 35 | +- The function takes an array as a parameter. |
| 36 | +- It creates a variable called trueName and assigns it a value of an empty array. |
| 37 | +- It creates a for loop that iterates through the array. |
| 38 | +- It checks if the element ends with ".js". |
| 39 | +- If it does, it pushes true to the trueName array. |
| 40 | +- If it doesn't, it pushes false to the trueName array. |
| 41 | +- It returns the value of trueName. |
| 42 | + |
| 43 | +## References |
| 44 | + |
| 45 | +- [Wikipedia](https://en.wikipedia.org/wiki/For_loop) |
| 46 | +- [GeeksforGeeks](https://www.geeksforgeeks.org/for-loop-in-javascript/) |
| 47 | +- [StackOverflow](https://stackoverflow.com/questions/1669190/javascript-min-max-array-values) |
| 48 | + |
| 49 | +## Problem Added By |
| 50 | + |
| 51 | +- [GitHub](https://www.github.com/devvsakib) |
| 52 | +- [LinkedIn](https://www.linkedin.com/in/devvsakib) |
| 53 | +- [Twitter](https://twitter.com/devvsakib) |
| 54 | + |
| 55 | +## Contributing |
| 56 | + |
| 57 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 58 | + |
| 59 | +Please make sure to update tests as appropriate. |
0 commit comments