I have been searching but I cant get a clear answer. (I am a beginner in javascript and this is a test/practice inside the console) How can I make a local scope within a for loop, a global scope so I can then add it to variable with an array of objects. For Example : I want the user to set how many stars(rating) a movie has inside a variable(prompt) named "stars". Then be able to call it inside the movieDb variable inside the object/array where it says "rating :" ... using the variable stars so it can console log the stars the user inputs along with the other information. Is this allowed or is there a different approach?
var movieDb =[
{
haswatched : "i",
title : "In Burges",
rating : stars,
},
{
haswatched : "i",
title : "Frozen",
rating : stars,
},
{
haswatched : "i",
title : "Mad Max",
rating : stars,
},
]
for (var i = 0; i < movieDb.length ; i++) {
var w = prompt("did you watch the movie?")
if (w === 'yes'){//first IF
movieDb[i].haswatched = "you have watched";
var r = prompt("would you like to rate us?");
if (r === "yes") {//nested in first IF
var stars = prompt("Enters Stars");
alert("Thanks for rating & Watching!")
} else if(r === "no"){ //nested in second IF
alert("You did not rate, Thanks for watching!")
}
}
else if (w === "no"){ //first ELSE IF
movieDb[i].haswatched = "you have not seen";
}
console.log(movieDb[i].haswatched + " " + "\""+movieDb[i].title+"\"" + " -" + movieDb[i].rating);
}'
I think if I understand what you are trying to achieve, this should help ...
var movieDb = [
{
haswatched : "i",
title : "In Burges",
rating : 0,
},
{
haswatched : "i",
title : "Frozen",
rating : 0,
},
{
haswatched : "i",
title : "Mad Max",
rating : 0,
},
];
for (var i = 0; i < movieDb.length ; i++) {
var w = prompt("did you watch the movie " + movieDb[i].title + "?")
if (w === 'yes'){//first IF
movieDb[i].haswatched = "you have watched";
var r = prompt("would you like to rate us?");
if (r === "yes") {//nested in first IF
var stars = prompt("Enters Stars");
movieDb[i].rating = stars;
alert("Thanks for rating & Watching!")
} else if(r === "no"){ //nested in second IF
alert("You did not rate, Thanks for watching!")
}
} else if (w === "no"){ //first ELSE IF
movieDb[i].haswatched = "you have not seen";
}
}
console.log(movieDb);