Javascript : check if a word contains a char in a text file

JavaScript Experiment #1

The goal for this was to read a txt file, go through each word of that txt file, and make a string from every word in that txt file that contain the letter 'o'.

*"Halt!" and "hand.]"   are in there because in my list made from the text file:
       'down.]\nHalt!' --- is the item in the list.  It's printing the newline. So
        they appear seperated.
        Same with hand --     'hand.]\nWhoa',




There were many many answers that weren't exactly what I was looking for, but gave me the breadcrumb to follow to the next search I needed to do.



This was my extension to an exercise from Learn JavaScript the Hard way.
Shaving the yak is more fun if you get to give it a fun haircut before the real work starts. (*draws a maze in the yak fur with shears for my toy soldier to tromp through*)

Some things I found out:
1)
(some string) = string   (x = char to look for)
Javascripts string.includes('x') returns a boolean.
*That took me a bit.  Why is it telling me everything is -1  !!

2)
It isn't all like python, but there is so much similarity, I've found sometimes the answers are as simple as they are in python, just figure out what to type to do the same thing.

3)
JavaScript seems to need anything it can't immediately identify as a string, converted to a string to perform string operations on it.
So although my text file contains text, when it reads the file, the items in the file are not strings until we convert them as such.
A link explaining string operations in JavaScript:
w3schools string-methods


4)
getting all the markers in the right place is going to be the most difficult part.
what i mean by 'markers' -->   : ; () {} [] ""   all those key values that the computer can read as a command.    function()  dictionary{}   this_is_a_command:    this_is_end_of_command;  "string"  ect....
They probably have a specific name.  Not sure how to google 'those special items in code that tell the computer a command in one keystroke'.

** by the way google gave me keyboard shortcuts when I googled exactly that

If you know the word I'm looking for, I'd really appreciate it dropped in the comments!!!

w3schools if-else

5)
console.log() is my print statement.
much love for that while I learn!!!!
I seen examples in stackOverflow that use
print(variable)  but when I tried to use print, it threw errors.
maybe I'll look that up and update.

And if you're just here for some code:
// the text.txt is a snippet of monty python script I have in a file

#####  extension_exercise.js  #####


 // https://nodejs.org/api/fs.html
const fs = require("fs");
let contents = fs.readFileSync("test.txt");

//make newstring contain something so when testing
//I can see if it's doing something instead of empty space

var newstring = " x ";
// from the exercise:  commented out
//console.log("Contents:");
//console.log(contents.toString());


// using a callback
console.log("----------");
// I don't use the err in the exercise, but I will be looking up how
// to properly handle errors next
fs.readFile("test.txt", (err, data) => { //1 <-- open order
  // this is my experiment to see if I can splice wierd sentences
  // together from words in a txt file.
 
  // loop through text looking for word containing char 'o':

  var text = data.toString();
    //console.log(text);
  var split_text = text.split(" ");
    //console.log(split_text);
  for (i = 0; i < split_text.length; i++){ //2
    var word = split_text[i];

    //console.log(word);
    if (word.includes('o')) { //3
      newstring += " " + word
    } //3 <-- close order
  } //2
  var give_it_space = "\n\n\n\n\n\n" // to get a good pic for blog
  var pretty_string = give_it_space + newstring + give_it_space
  console.log(pretty_string)

}); //1










Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame