1

1. I want ratingClass = 'fresh' if rating > 59 OR if audience_rating > 70. How? I tried with

if (rating > 59) || (audience_rating > 70) {

 var ratingClass = 'fresh';

Here's the code:

 if (rating > 59) {
    var ratingClass = 'fresh';
 } else if (rating > 0){
    var ratingClass = 'rotten';
 } else {
   var ratingClass = 'na';
 }
 if (audience_rating > 59) {
    var audienceClass = 'fresh';
 } else if (audience_rating > 0){
    var audienceClass = 'rotten';
 } else {
    var audienceClass = 'na';
 }
 $parentEl.addClass(ratingClass);

2. In line 114 of http://pastebin.com/UN8wcB7b I get Uncaught TypeError: Cannot read property 'length' of undefined every ~3 seconds when hideRotten = true. Is it easily fixed and/or do I need to worry about it at all?

I am new to JavaScript coding, currently I am trying to learn by doing. Can you recommend any resources to learn writing Chrome Extensions with JavaScript?

Thanks :-)

2
  • Change: if (data.movies.length > 0) { => if (data.movies && data.movies.length) { to avoid errors if there are no movies. Commented Jul 6, 2013 at 13:24
  • As a sidenote, there is no need for redeclaring the variables in each if block. Once declared, it is visible in all the blocks, regardles of the if result. Commented Jul 6, 2013 at 14:39

3 Answers 3

1

it's because you are trying to read the length of a null element. So put a condition to test if movies are null in your if statement on line 114 so it says something like

if(data.movies && data.movies.length > 0)

Though if you're setting some data in this if statement that will be used other places in the code you may have to put checks like this in other places as well to completely avoid this type of problems.

Sign up to request clarification or add additional context in comments.

Comments

1

The error definitely means

typeof data.movies === "undefined"

to avoid this I will recommend

...

$.getJSON(movieUrl, function(data){
    // data can be undefined becoz of various reasons and so is data.movies
    if(!(typeof data === "undefined") && !(typeof data.movies === "undefined")) {
    //put similar checks in ur code

...

Comments

1

1) the condition after the if must always be completely surrounded in brackets:

// wrong
if (rating > 59) || (audience_rating > 70) {

// has to be:
if ( rating > 59  || audience_rating > 70 ) {

or if you are unsure about the operator precedence:

if ( (rating > 59)  || (audience_rating > 70) ) {

2) You have to check first, if the movies attribute exists in your data respone (Because if it doesn't, you also can't call length on it):

// can throw error if data.movies === undefined
data.movies.length > 0

// the safe way, check data.movies first:
if (data.movies && data.movies.length > 0)

this is pretty much equivalent to the long version*:

if (typeof(data.movies) === `undefined` && data.movies.length > 0)

* Not exactly, read this article why

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.