Write JavaScript code that checks if the local storage contains an item named au
ID: 3749714 • Letter: W
Question
Write JavaScript code that checks if the local storage contains an item named authToken. If the item exists, the code should assign the variable userAuthToken with the item. (2.1) (2.2) Write a regular expression that exactly matches a string consisting of a city, state and ZIP code. The city must be followed by a comma (), the state must be two capital letters, and the ZIP code must be 5 digits optionally followed by a hyphen (-) and four digits. Ex: "Tucson, AZ 85721" matches, whereas " Tucson AZ 85721" does not match (2.3) Write a MongoDB/Mongoose query to find all Review documents with a rating greater than 2Explanation / Answer
1) Checking if localStorage has authToken and assigning to userAuthToken if present
code:
var userAuthToken = null;
if (window.localStorage.getItem('authToken') !== null) {
userAuthToken = localStorage.getItem('authToken');
}
2) Regex:
The regex is ^[a-zA-z]+,s[A-Z]{2}s[0-9]{5}(-[0-9]{4})?$
Explanation:
^ is start
[a-zA-z] is matching the city and + is one or more occurances
s is space
[A-Z]{2} is exact two capital letters
[0-9]{5} is matching zip code
(-[0-9]{4})? is optional hyphen followed by 4 digit number
3) Mongo query:
db.Review.find({"rating":{$gt:2}}) and if you want it to be printed neatly use pretty() as follows
db.Review.find({"rating":{$gt:2}}).pretty()
Happy coding !!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.