Mini Linter step 5


#1

Hi All,

Here is the link to the Mini Linter [project]

I was able to complete this project with the help of the forums. However, I am confused about step 5. The hint says to use this code:

let sentences = 0;
betterWords.forEach(word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!') {
    sentences+=1;
  }
});

I do not understand why I use ‘[]’ in the if statement after ‘word’?

I was also confused during this project because it asked me to use formatting for iteration (like the above) that I had not learned about previously. Am I missing something? Or is there a manual/dictionary somewhere where I can view different ways to write iterators and other code?


#2

square brackets are used to access by index for string and array:

let myWord = "hello world"
console.log(myWord[0]) // h
let myArray = ['a', 'b', 'c']
console.log(myArray[1]) // b

word is a string, so we access the string by index.

how would you have done it then?

each programming language has extensive documentation. For JS, MDN has excellent documentation


#3

In programming, data objects take two basic forms:

  • non-iterable
  • iterable

Non-iterable objects are not subscriptable, meaning they cannot be traversed. Examples of these are,

  • Number
  • Boolean
  • Function
  • null
  • undefined

If we attempt to access an index of any of the above the result may be, ‘undefined’.

let a = 42;
console.log(a[0]);    // ->  undefined

The square bracket syntax is known as subscript notation. In the above example, a is the identifier, and [0] is the subscript, or as mentioned above in @stetim94’s reply, the index.

Subscriptable (namely, iterable) objects in JavaScript include,

  • String
  • Array
  • Object
  • Map
  • Set

These objects are all containers of sorts, and with exception to strings, are known as data structures. We access the data points by their subscript:

let s = "42";
console.log(s[0]);        // ->  4
console.log(s[1]);        // ->  2

let a = [1, 2, 3, 4];
console.log(a[3]);        // ->  4

let o = { name: "Joe", age: 19 };
console.log(o['age']);    // ->  19

If per chance you might have jumped ahead into a project before completing the introductory units of the track, then it would be advisable to go back and start at the beginning so you get more hands-on practice with data types.

May we assume you are unfamiliar with arrow function notation?

const myFunc = (args) => {
    // code block (function body)
};

The more familiar form looks like,

var myFunc = function (args) {

};

and its declarative form,

function myFunc(args) {

}

Arrow functions are essentially the same as function expressions with two exceptions:

  • no this object
  • no arguments object

and they have some built in optional shortcut features:

  • single line functions do not need to be enclosed in curly braces
  • single line functions have an implicit return
  • single parameter does not need parens

Eg.

const timesFive = x => x * 5;

console.log(timesFive(42));    // ->  210

In ES5 form,

var timesFive = function (x) {
    return x * 5;
};

#4

Thank you! This is very helpful. I understand step 5 now. I am still not sure if I would be able to come to that conclusion on my own. Do you have any suggestions as to how I can gain more practice to be able to solve problems like step 5 on my own? I am doing the java script course on codeacademy.


#5

Mini Linter

  1. Now, count how many sentences are in the paragraph.

This may seem tricky, but remember that all of the sentences in this paragraph end with a period (.) or an exclamation mark (!). You could iterate over the array and add 1 to a sentence counter variable for each word that has a period or exclamation mark as its final character.

Your code is using an arbitrary index to find the closing character of a word, and that’s pretty bang on. JavaScript gives us a more data determined String/Array method to find the index of a target character or value…

String.indexOf(value)

The following model uses a defined data set to filter out of the inputs…

let fullStops = 0;
for (let token of ['.',':','?','!']) {
  fullStops += betterWords.filter(word => word.indexOf(token) > -1).length;
}

console.log(fullStops);    // ->  12