Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Exporting data from a web browsers console to either the clipboard or a text file

I have written a function like so:

function wordSnatcher(element, startsWith) {
    var arrOfElem = document.getElementsByTagName(element);
    var nwArr = [];
    var tmp = "";
    for (var i = 0; i < arrOfElem.length; i++) {
        tmp = arrOfElem[i];
        if (tmp.textContent.charAt(0).toLowerCase() ==  startsWith.toLowerCase() && 
             tmp.textContent.length > 3) {
            nwArr.push(tmp.textContent);
        }
    }
    return nwArr.sort();
}

How this function would be implemented would look like this:

wordSnatcher('li', 'b');

The end result is an HTMLCollection of all words that start with the specified letter and reside in the specified element when executed in the console of a web browser.

So far I've been storing the array in a variable like this:

var b = singleWordSnatcher('li', 'b');

But this only gets me so far... I've been racking my brain trying to figure out how I can take this array and move it into an external JavaScript file. I can't copy it from the console using the clipboard, and I also can't seem to export it to a medium where I can grab it using the clipboard (like a .txt file)

If you merely iterate over the array and log it to the console it seems to, for some reason, only print out a portion of the arrays values

Besides that sort of defeats the purpose of what I'm trying to do because I want to keep it formatted as an array.

If you would like to try it exactly as I am trying it, use this function in the console while on this page: http://www.scrabblefinder.com/starts-with/b/

Doing this yields an array of over 5000 words, which is not easily manageable

1 Answer

There's no way to write a file with JS. You will need to implement a server side language to write files to the local computer. If you can have a master word file you can read in that file then use the data as needed.