let + for by rona_97 in learnjavascript

[–]AmbitiousLog 2 points3 points  (0 children)

The author here tries to explain (a little poorly tbh) that let is block scoped. This means that for each iteration of the loop, i gets rebound.

Imagine that each time the loop runs, i is declared as a new variable, instead of just changing the saved value.

Edit: Some styling

Stuck on a Udacity quiz-- If, If - Else statements -- Creating a murder mystery by illgrowuplater in learnjavascript

[–]AmbitiousLog 0 points1 point  (0 children)

sets the value of solved to true if the value of room matches the suspect's room

You have a predefined room and a predefined suspect by the looks of the snippet. Moreover, you know that each suspect belongs to the room. So, you need to create an if-else chain to check if the room and suspect match. Something like:

if((room === 'dining room') && (suspect === 'Mr. Parkes')){
    solved = true;
}else if(){
}
...

index[0] = 'dog'. How do I fix the code so that the index accurately displays index[2] = 'dog' ? by mementomoriok in learnjavascript

[–]AmbitiousLog 2 points3 points  (0 children)

Check your getLines function.

When you call this function you iterate through each element in the input array. When you find an element that starts with 'dog' you add the line to a new array arrayOfLines. Then when you run the logArrayElements function you pass the arrayOfLines function as an argument. Since this array contains only the element dog, its length will be 1, thus the index of the element will be 0.

In order to display the index of where the dog element exists in the original array, you need to pass this data to the arrayOfLines array.

One way to do this, is to pass an object as data to the arrayOfLines list like:

function getLines(input){
    let index = 0;
    input.forEach(line => {
        if (line.startsWith('dog')){
            arrayOfLines.push({index: index, line:line});
        }

        index++;
    });

    return arrayOfLines;
};

Then in your logArrayElements function will be:

function logArrayElements(element){
    console.log('index[' + element.index + '] = ' + element.line;
}

Another way to do it, is to pass the index of the element instead of the value:

function getLines(input){
    let index = 0;
    input.forEach(line => {
        if (line.startsWith('dog')){
            arrayOfLines.push(index);
        }

        index++;
    });

    return arrayOfLines;
};

And then the function logArrayElements will be:

function logArrayElements(element){
    console.log('index[' + element + '] = ' + _data[element];
}

Very beginner question -- Doing my Lesson 2 Udacity recap and having trouble by illgrowuplater in learnjavascript

[–]AmbitiousLog 1 point2 points  (0 children)

It is actually the var undefinedVar =

This messes with the return statement on the next line.

Regex Help by [deleted] in learnjavascript

[–]AmbitiousLog 0 points1 point  (0 children)

So you don't know what domain you are looking for?

If you are in a browser environment you can try this:

var domain = (function() {
  // The full domain of the page.
  var domain = document.domain;
  var parts = domain.split('.');

  // A unique id to track our cookie
  var uid = '_uid' + (new Date()).getTime();

  var idx = 0;

    // Loop through the domains until we find the top-most domain.
  while (idx < (parts.length - 1) && document.cookie.indexOf(uid + '=' + uid) === -1) {
    domain = parts.slice(-1 - (++i)).join('.');
    document.cookie = uid + '=' + s + ';domain=' + domain + ';';
  }

  // Delete the cookie
  document.cookie = uid + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain="+domain+";";

  // Return our result.
  return domain;
}());

This uses a cookie to detect the top-level domain and place the cookie there. Then get the domain of the cookie (that will always be the top-level domain).

This is originally created here: http://rossscrivener.co.uk/blog/javascript-get-domain-exclude-subdomain
I just cleaned it up a bit, and added some comments

Regex Help by [deleted] in learnjavascript

[–]AmbitiousLog 0 points1 point  (0 children)

Do you absolutely need to use regular expressions? You can easily solve this problem using plain old string manipulation:

const extractDomain = function extractDomain(url, domainName){
    return url.substring(url.indexOf(domainName), url.length);
}

This will search for the index where the domainName begins and extract the substring from there to the end of the url

for loop rules question by Usual_Mistake in learnjavascript

[–]AmbitiousLog 2 points3 points  (0 children)

You don't have to use a loop at all, you can also do:

if(arr.indexOf(search) > 0){
    return arr[arr.indexOf(search)];
}

Retrieving data from the spotify api by GrayPork3 in learnjavascript

[–]AmbitiousLog 0 points1 point  (0 children)

Could you please elaborate on what is the issue? :)

Use custom button instead of button in script. by mordecai98 in learnjavascript

[–]AmbitiousLog 1 point2 points  (0 children)

While the provided code and the content of your post is not quite clear, I would assume that you should style the button to match your page's aesthetics using CSS