How long is a piece of string :)

I am getting back into coding for my own amusement. So far out of it, I may as well have been putting holes in stone tablets, never mind punch cards. I would like to recreate a fortune teller program but all I can do so far with javascript is mildly insult a user based on their choice of colour :slight_smile: Is javascript the right language if I want to take a user input, of finite (defined by me) but uncontrolled length and content, and look for a specific word(s) within the input string. Based on the appearance of a specified word(s) it should spit out a suitably ridiculously insightful answer. While I can probably get there using any language eventually I fear I have not got that much time :slight_smile: All assistance welcome.

2 Likes

There is no right answer. If you wish to do this in a browser with relative ease and no other software but a text editor, then JavaScript is the choice. That would assume you have some HTML and CSS in your knowledge base, though at first even that could be ignored (but only for a time). The console is fine and good for our own use, but to subject a user to it or JS dialogs is to my mind outright abuse. We will need to bring HTML into the picture at some point so the code can run in a browser window, not the console. User interaction would be clicks and text boxes.

Even if your aspirations are to go with another language, it would be a very worthwhile venture to go down this path now, rather than later. It will mean so much moving forward because you will carry some knowledge of how a browser works, the DOM, and handling user interaction within an interface. It’s a great experience to have under your belt, and a great experience on the whole.

On the topic of your project, start by tossing around some ideas and write them down. Set limits and constraints, but otherwise give some operational latitude. That means having perhaps a data store of string expressions, emoji escape secquences, etc… It also implies collecting user data through a series of inputs. Form controls in a user interface are so much easier to handle and write for.

In an straight console program, fifteen input statements would need to execute in order to obtain a requisite fiften data points. A web form with fifteen fields means the user is free to fill then in at will, and in any order. And, the prompt expressions are stored in the HTML, not the JS, a big chunk lobbed off the program overhead.

But enough about your project. Re-iintroduce yourself to the HTML/CSS?JavaScript path and prepare to have lots of fun.


Technical stuff

<input id="qid01">
<p></p>

The above form control written anywhere in an HTML document would present as a text input field in the normal flow of the page. I’m guessing the size, but somewhere between 20 and 40 characters wide, and 1 high.

There is no required structure to contain this line. It is perfectly valid all on its own. Everything is good. We can even style it to suit our design needs.

The id attribute is a hypertext referenceable object. That means this fragment of code can be requested in a URL. I like to call it a fragment identifier that a hook reference anywhere in the page can immediately navigate to.

<a href="#qid01">01</a>

Those hooks are not just in the HTML or request URL’s from the outside, they are also in the CSS and the JavaScript.

#qid01 {

}

and,

let qid01 = document.querySelector('#qid01');

qid01.addEventListener('change', function(e)) {
  // write input value to p-node immediately following in normal flow
  this.nextSibling.textContent = this.value;
});

You may be a while getting to the point where this all makes sense, but expect it to be in the wheelhouse of your expectations and needs.

1 Like

I am just looking into this site, and am NOT happy with it as one needs YEARS of practice with this , and they post one can learn “without years of tutoring.” Kelly is using terms like JS dialogs, how a browser works, the DOM, user interface, and emoji escape sequences. These terms are NOT for a “lay person” that does NOT have at least 5 yrs practice. I would post a big WTF trying to understand this! This brings on ANXIETY in students!

1 Like

Well, fortunately it is not how the curricullum presents. I, however, was convinced that you had some previous knowledge. My mistake in that gaff. Sorry.

Still, this is what we are working toward, and when it makes sense to us, we know we are getting somewhere. “Let thy reach be farther than thy grasp; else, what’s a heaven for?”


Addendum

As they were contemporaries it is easy to expect they might juxtaposition on one another’s phrases. The above is Tennyson paraphrasing Browning.

“Ah, but a man’s reach should exceed his grasp, Or what’s a heaven for?”

Or is it Browning paraphrasing Tennyson?

1 Like