I have to celebrate you baby, using a switch statement and still error


#1

I was doing the excercise and after finally getting it to return the review for the movie the right way, It gives me this response, "Oops, Try again. Better use a switch statement."

Am I not using this switch statement properly?

var toyStory2 = {
name: "Toy Story 2",
review: "Great story. Mean prospector."
};
var findingNemo = {
name: "Finding Nemo",
review: "Cool animation, and funny turtles."
};
var lionKing = {
name: "The Lion King",
review: "Great Songs."
};
var getReview = function (name) {
switch(name) {
case "Toy Story 2":
return toyStory2.review;
case "Finding Nemo":
return findingNemo.review;
case "The Lion King":
return lionKing.review
default:
return "review not listed"
}
};
getReview("Finding Nemo")

thank you in advance for the help!


#2

nevermind, I got it... the client didn't use the word "name" in the code it provided me, it used the word movie, so when I changed it, for some reason that mean't i didn't use a switch statement. either way, I can proceed now, just had to use "movie" as the variable instead of "name."

var toyStory2 = {
movie: "Toy Story 2",
review: "Great story. Mean prospector."
};
var findingNemo = {
movie: "Finding Nemo",
review: "Cool animation, and funny turtles."
};
var lionKing = {
movie: "The Lion King",
review: "Great Songs."
};
var getReview = function (movie) {
switch() {
case "Toy Story 2":
return toyStory2.review;
case "Finding Nemo":
return findingNemo.review;
case "The Lion King":
return lionKing.review
default:
return "review not listed"
}
};
getReview("Finding Nemo")


#3

You got the whole question messud up @webjumper26363. But no problem, read the instructions you dont need variables just switch statements returning strings like this:

case "Toy Story 2":
        return "Great story. Mean prospector.";
    case "Finding Nemo":
        return "Cool animation, and funny turtles.";
    case "The Lion King":
        return "Great songs.";
    default :
        return "I don't know!";

Tell me if it helped


#4

@amanuel2 O you are right! I made it more complicated than it needed to be! either way, it was good practice!

Thank you so much!


#5

Took so long to type I'm already late for the party. Oh well, it's supplemental stuff, anyway.

The added named objects are not asked for in the instructions, so it might be best to revert back to a more literal approach.

var getReview = function (movie) {
    switch (movie) {
    case "Toy Story 2":
        return "Great story. Mean prospector.": 
    case "Finding Nemo":
        return "Cool animation, and funny turtles.":
    case "The Lion King": 
        return "Great Songs.";
    default: return "I don't know.";
    }
};

However, if the SCT lets your code pass, then it is satisfied with the outputs. If you are going to use objects then organizing them into one object or array will keep the variables from cluttering up memory.

It may not have been covered yet, so let it slide if so, for now and come back later when the code is more familiar.

function Film(name, review){
    this.name = name;
    this.review = review;
}
function addFilm(name, review){
    films.push(new Film(name, review));
}
function getReview(movie){
    switch(movie){
    case films[0].name:
        return films[0].review;
    case films[1].name:
        return films[1].review;
    case films[2].name:
        return films[2].review;
    default: return "I don't know.";
    }
}
var films = [];
addFilm("Toy Story 2", "Great story. Mean prospector."); 
addFilm("Finding Nemo", "Cool animation, and funny turtles.");
addFilm("The Lion King", "Great Songs.");
console.log(getReview("Toy Story 2"));

This is where we see the switch as a dynamic branch is less practical. A for statement would be a better construct. But the above illustrates organizing like objects in a single data structure. The array is iterable so easy to work with when seaching for or querying data.

function getReview(movie){
    for (var i = 0; i < films.length; i++) {
        if (movie === films[i].name) {
            return films[i].review;
        }
    }
    return "I don't know.";
}

#6

@mtf , I see what you are doing, the prompt didn't actually mention how to arrive at the answer, it only specified not to use if/else because of its inefficiencies.

The "get stuck, hint" button mentions switches as an accepted method if I didn't choose other ways to reach the outcome, I'm pretty sure i could have used arrays as well and it would have been accepted.

I don't think i've reached the point where it specifies which clutters up memory less but thats interesting. I'll keep that in mind going forward.

I think I chose Objects simply because I was discussing literal object formatting vs constructor and which would be cleaner / more understandable and wanted some practice in that area.

thanks for the feed back, I really appreciate it.


#7

Generally speaking, can you imagine what a rat's nest it would be once there are a couple hundred arbitrarily named objects that we have to keep track of? Individual, short life named objects are infinitely useful, but when there are many floating around in global scope the obvious solution is to package them into a single reference object.

It is not a pressing concern at this point so do let this slide, only now you are a little wiser with respect to memory usage and efficiency. This learning is cummulative and the concern comes up frequently when writing resource intensive programs.

There is no need to rush through the early Objects lessons. If anything, take some time to play with each exercise and really get comfortable typing the variations, thinking while you type. What do I know/understand about new Object()? How does this compare to {}? Do I know the difference between property specific (dot notation) and key specific (bracket notation) identifiers? Do I know the difference between a literal and dynamic key?

Stream of consciousness while we think through our solution code. Eventually our intuition takes over and these things never come into question. Read, practice and review. The upcoming lessons will keep and mean more if the early ones do moving forward.

Just saying... Before I forget, good intuition reaching for the object. It is the right direction to look for a scalable working solution, as we have already pretty much confirmed. Keep thinking like that!


#8

Fixed the code above. My apologies. If you see wicked bad errors like this, try to think around them, and know that it is hypothetical, and not tested. I tend to rattle off things like this from time to time.


#9

Revision…

Code
function Film(name, review){
    this.name = name;
    this.review = review;
}
function addFilm(name, review){
    films.push(new Film(name, review));
}
/*
function getReview(movie){
    switch(movie){
    case films[0].name:
        return films[0].review;
    case films[1].name:
        return films[1].review;
    case films[2].name:
        return films[2].review;
    default: return "I don't know.";
    }
}
*/
function getReview(movie){
    for (var i = 0; i < films.length; i++) {
        if (movie === films[i].name) {
            return films[i].review;
        }
    }
    return "I don't know.";
}
var films = [];
addFilm("Toy Story 2", "Great story. Mean prospector."); 
addFilm("Finding Nemo", "Cool animation, and funny turtles.");
addFilm("The Lion King", "Great Songs.");
console.log(getReview("Toy Story 2"));

#10

I can only imagine the legend I might have to hold on to in order to remember what every object is/does!

Thanks again for all the insight, I will take my time and absorb all of this!


#11

To add to your wisdom, consider dependency. getReview() in whichever form is dependent upon films being a defined array with defined objects, with defined keys.

We free our functions of dependency when we make them generic. Already we have hinted at that with the local variable, movie. Our data creation process has thus far gone with film. When compared it is to a fixed reference, films which introduces dependency. This is what we need to change.

To open things up we need another parameter, an array object.

function getReview(movie, vault){
    for (var i = 0; i < vault.length; i++) {
        if (movie === vault[i].name) {
            return vault[i].review;
        }
    }
    return "I don't know.";
}
//console.log(getReview("Toy Story 2"));
console.log(getReview("Toy Story 2",films));

Given any object that falls within the parameters of our program, this is a generic solution, The only proviso is that 'vault' cannot be a globally defined variable. It has to be unique to local scope since it is an object reference, not a value.


#12
Code
function Cinema(){
    this.catalog = [];
}
function Film(name, review){
    this.name = name;
    this.review = review;
}
/*
function addFilm(name, review){
    films.push(new Film(name, review));
}
*/
Cinema.prototype.addFilm = function (name, review) {
    this.catalog.push(new Film(name, review));
};
Cinema.prototype.getReview = function (movie){
    var object = this.catalog;
    for (var key in object) {
        if (movie === object[key].name) {
            return object[key].review;
        }
    }
    return "I don't know.";
};
/*
function getReview(movie){
    switch(movie){
    case films[0].name:
        return films[0].review;
    case films[1].name:
        return films[1].review;
    case films[2].name:
        return films[2].review;
    default: return "I don't know.";
    }
}
*/
/*
function getReview(movie){
    for (var i = 0; i < films.length; i++) {
        if (movie === films[i].name) {
            return films[i].review;
        }
    }
    return "I don't know.";
}
*/
/*
function getReview(movie, vault){
    for (var i = 0; i < vault.length; i++) {
        if (movie === vault[i].name) {
            return vault[i].review;
        }
    }
    return "I don't know.";
}
*/
/*
function getReview(movie, vault){
    for (var key in vault) {
        if (movie === vault[key].name) {
            return vault[key].review;
        }
    }
    return "I don't know.";
}
*/
//var films = [];
var films = new Cinema();
films.addFilm("Toy Story 2", "Great story. Mean prospector."); 
films.addFilm("Finding Nemo", "Cool animation, and funny turtles.");
films.addFilm("The Lion King", "Great Songs.");
/*
addFilm("Toy Story 2", "Great story. Mean prospector."); 
addFilm("Finding Nemo", "Cool animation, and funny turtles.");
addFilm("The Lion King", "Great Songs.");
*/
//console.log(getReview("Toy Story 2"));
//console.log(getReview("Toy Story 2",films));
console.log(films.getReview("Toy Story 2"));

This gives us the ability to categoize film libraries in an inifinite number of ways, not the least of which will be “Mysteries”. We can add a sub-genre as applies. The protoype environment affords all kinds of possibilites when it comes to methods.


#13

We’re getting closer to relational database interaction with this…

Code
function Cinema(rating){
    this.rating = rating;
    this.catalog = [];
}
function Film(name, review){
    this.name = name;
    this.review = review;
}
Cinema.prototype.addFilm = function (name, review) {
    this.catalog.push(new Film(name, review));
};
Cinema.prototype.getReview = function (movie){
    var object = this.catalog;
    for (var key in object) {
        if (movie === object[key].name) {
            return object[key].review;
        }
    }
    return "I don't know.";
};
var pg = new Cinema('PG');
pg.addFilm("Toy Story 2", "Great story. Mean prospector."); 
pg.addFilm("Finding Nemo", "Cool animation, and funny turtles.");
pg.addFilm("The Lion King", "Great Songs.");
console.log(pg.getReview("Toy Story 2"));