Reading Eloquent JavaScript...can I ever get this good?

after reading chapter 5 of Eloquent JavaScript I’m actually shocked at the level of sophistication in the code written there. The way the author puts together the logic/sequences of thought in his code is nothing short of art.

I guess what I want to ask is - is it common for developers to be able to write code to that level of eloquence with some experience (and if so, typically after how long?) or is the code displayed in the book something only writeable by coding prodigies/ super talented CS grads?

I haven’t read the book so cannot judge. Can you give some examples of the sophistication?

a program to take a string and identify what percentage of whatever Unicode scripts are present in it out of the total. the program also accounts for Unicode characters which take up more than one code unit

each unicode script is modelled as such and placed in an array called SCRIPTS:

{
  name: "Coptic",
  ranges: [[994, 1008], [11392, 11508], [11513, 11520]],
  direction: "ltr",
  year: -200,
  living: false,
  link: "https://en.wikipedia.org/wiki/Coptic_alphabet"
}
function characterScript(code) {
  for (let script of SCRIPTS) {
    if (script.ranges.some(([from, to]) => {
      return code >= from && code < to;
    })) {
      return script;
    }
  }
  return null;
}

function countBy(items, groupName) {
  let counts = [];
  for (let item of items) {
    let name = groupName(item);
    let known = counts.findIndex(c => c.name == name);
    if (known == -1) {
      counts.push({name, count: 1});
    } else {
      counts[known].count++;
    }
  }
  return counts;
}

function textScripts(text) {
  let scripts = countBy(text, char => {
    let script = characterScript(char.codePointAt(0));
    return script ? script.name : "none";
  }).filter(({name}) => name != "none");

  let total = scripts.reduce((n, {count}) => n + count, 0);
  if (total == 0) return "No scripts found";

  return scripts.map(({name, count}) => {
    return `${Math.round(count * 100 / total)}% ${name}`;
  }).join(", ");
}

console.log(textScripts('英国的狗说"woof", 俄罗斯的狗说"тяв"'));
// → 61% Han, 22% Latin, 17% Cyrillic

Well I presume it is explained in the book but looking at it, it is hard to read as there are no comments anywhere.

I can see he has used as many different methods as possible I guess to illustrate what each can do.

Like with any problem it is a case of breaking it down to simple tasks.
If you want to improve your coding I would recommend a site like codewars.com.

1 Like

Hi,

Actually many people complain about how hard the book gets after chapters 5 or 6. I read many chapters myself and yes, you freak out when you can’t follow those examples. But this is just like any language: when you begin to learn it, everything looks difficult as hell and you think you won’t be able to speak that language unless you know every word or expression. The reality is very different, though: once you get up and running with it, you will be able to move forward finding whatever you need to express more clearly one day after another. In Javascript terms, you have many resources to practice your logic and knowledge so you can improve your habilities with the language.

As of the time taken to get to this level… I guess it’s like everything else in this world. You just practice and you’ll find out yourself :slight_smile:

Happy coding!

1 Like

thank you very much, ill keep pushing on

2 Likes

I noticed myself getting stuck on exercises and what I’d do is repeat over and over until I get it. I would read other’s codes, comments, check other sites’ definitions to get a better perspective until I truly get it. I realize the examples can be hard but there’s a reason why this book is considered to be one of the best.

One thing to remember… It may have taken an hour or two to write the code. It also takes awhile to understand the code once you first look at it. Don’t be discouraged.

That book is not for everyone, including me :wink: and many others too don’t like it…

A Better and Practical alternative for Eloquent Javascript is The Javascript Way followed by Programming for the Web with JavaScript

1 Like

I’m also stuck on the Dominant Writing Direction problem in Ch.5. It’s not just the complexity of the code, but also understanding what the question is asking and understanding unicode. I think it’s doable step by step, but it’s just hard managing all the information in one run as the brain isn’t used to keeping track of all these different variables and nested data, etc. So I’m kind of stuck on what the question is actually asking as I even see one parameter example with two languages in it…??

@rrogerthat

I don’t want to spoil an answer for you, but if you want to take a look at my solution let me know.

I believe the authors intent was for you to use the countBy and characterScripts function. If you do so you don’t have to write much of your own code, but can build (refactor) on his examples.