unexpected token: identifier
i've changed the name of the constructor but nothing happens
// this is my javascript code
var budgetController = (function() {
var Expense = function(id, description, value) { //Constructor for the EXPENSE which starts from Capital letter
this.id = id;
this.description = description;
this.value = value;
};
unexpected token: identifier
You're not closing it ()
var budgetController = (function() {
var Expense = function(id, description, value) { //Constructor for the EXPENSE which starts from Capital letter
this.id = id;
this.description = description;
this.value = value;
})(); //Here's the problem
Related
I have a function that I have modified from some other code, and I am confused. With the 'else if' it seems that the 'Income' object only returns three attributes, rather than the four I need (I can return the value parameter, or I can use the value parameter in a calculation to a different value, but I can't capture both).
return {
addItem: function(type, des, val, price){
var newItem, ID;
if(data.allItems[type].length > 0){
ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
}else{
ID = 0;
}
if (type === 'exp'){
newItem = new Expense(ID, des, val);
}else if (type === 'inc'){
var ben = val * price;
newItem = new Income(ID, des, val, ben);
}
data.allItems[type].push(newItem);
return newItem;
},
I think my problem lies with this function, but as I say, I am now very confused. Is there an obvious problem with it?
Edit: this is the Income function constructor:
var Income = function(id, description, value, price){
this.id = id;
this.description = description;
this.value = value;
this.price = price;
this.ben = ben;
};
If you want to track ben, you can add it to the constructor args.
var Income = function(id, description, value, price, ben){
And add price when you instantiate a new Income object.
newItem = new Income(ID, des, val, price, ben);
My html page is not responding to this code I wrote in JS, i'm a total beginner, and just started learning JS, can somebody tell me why this doesn't work?
/* this is a practice file that'll play with js
nothing strange to look at here folks! */
var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;
function Hotel(HotelName){
this.HotelName = HotelName;
this.numRooms = 20;
this.numGuests;
this.checkAvailability {
if(numRooms != 20 ){
return true;
}
else{
return false;
}
}
this.getHotelName = function(){
//can it work with this dot operator?
return this.HotelName;
}
}
var HiltonHotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = getHotelName();
var el = document.getElementById('name');
el.textContent = fullName;
<!DOCTYPE html>
<html>
<body>
<div id = 'greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id = 'hotelName'>Hyatt</span>
</div>
<script
src = "https://stacksnippets.net/js">
</script>
</body>
</html
I'm pretty sure it's ordering and my syntax i need to work on, any advice is greatly appreciated thank you!
Few misunderstandings:
checkAvailability is a function, you are missing parens.
while accessing the getHotelName function, you have to refer to the HiltonHotel variable, to be able to access and call that function.
few minor errors in your html code, while operating in code snippet, you don't have to add a separate script, it's connected together by default.
var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;
function Hotel(HotelName) {
this.HotelName = HotelName;
this.numRooms = 20;
this.numGuests;
this.checkAvailability = function() { // it's a function (missing parens)
if (numRooms != 20) {
return true;
} else {
return false;
}
}
this.getHotelName = function() {
return this.HotelName;
}
}
var WeiHotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = WeiHotel.getHotelName(); // refer to the `WeiHotel` variable
var el = document.getElementById('name');
el.textContent = fullName;
<div id='greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id='hotelName'>Hyatt</span></h1>
</div>
An extension to the answer of #KindUser:
You're not using closures anywhere in this class to store some private state. Therefore you should attach the methods to the prototype and not to the instance itself. It's more economic, because now all instances share one function, not one per instance. And the JS engine can optimize that better.
Then, you have another error in checkAvailability: numRooms needs to be addressed as this.numRooms because it is a property of this instance, and there is no variable with this name.
And one about style. If you have something like
if(condition){
return true;
}else{
return false;
}
you can simplify this to:
return condition;
//or if you want to enforce a Boolean value,
//but your condition may return only a truthy/falsy value:
return Boolean(condition);
//sometimes also written as:
return !!(condition);
Next. Stick to the coding standards. In JS a variable/property starting with an uppercase letter would indicate a class/constructor, therefore HotelName, HiltonHotel, WeiHotel are misleading.
And I find the property name hotelName redundant and counter-intuitive. Imo you have a Hotel, it has a name, but that's just an opinion.
var firstName = 'Steven';
var lastName = 'Curry';
var fullName = firstName + ' ' + lastName;
function Hotel(name) {
this.name = name;
this.numRooms = 20;
this.numGuests;
}
Hotel.prototype.checkAvailability = function() {
return this.numRooms !== 20;
}
Hotel.prototype.getHotelName = function() {
return this.name;
}
var hotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = hotel.getHotelName(); // refer to the `weiHotel` variable
var el = document.getElementById('name');
el.textContent = fullName;
<div id='greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id='hotelName'>Hyatt</span></h1>
</div>
or as an ES6 class (and some playin around):
class Person{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
//this is a getter, you can read it like a property
get fullName(){
return this.firstName + " " + this.lastName;
}
//this function is implicitely called whenever you try to convert
//an instance of `Person` into a string.
toString(){
return this.fullName;
}
}
class Hotel{
constructor(name) {
this.name = name;
this.numRooms = 20;
this.numGuests;
}
checkAvailability() {
return this.numRooms !== 20;
}
getHotelName() {
return this.name;
}
}
var steve = new Person('Steven', 'Curry');
var hotel = new Hotel('Hilton');
var hName = document.getElementById('hotelName');
hName.textContent = hotel.getHotelName(); // refer to the `weiHotel` variable
var el = document.getElementById('name');
el.textContent = steve.fullName;
//this uses the `toString()` method to convert the `Person` steve into a string
//for people, this makes sense, for the Hotel you'd want to think:
// - where do I want to use this?
// - and what should this string contain?
console.log("Hello, I'm " + steve + " and I'm at the "+ hotel.name);
<div id='greeting'> Hello
<span id="name">friend</span>!
<h1>Welcome To the <span id='hotelName'>Hyatt</span></h1>
</div>
My code is
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data);
weatherData.temperature = data.main.temp;
updateWeather(weatherData);
function Weather(cityName, data) {
this.cityName = cityName
data.weather[0].description = descriptions;
this._temperature = '';
}
function updateWeather(weatherData) {
weatherCity.textContent = weatherData.cityName;
weatherDescription.textContent = weatherData.descriptions;
weatherTemperature.textContent = weatherData.temperature;
loadingText.style.display = 'none';
weather.style.display = 'block';
}
i get the error descriptions is not defined. works fine if i do this
var data = JSON.parse(http.responseText);
var weatherData = new Weather(cityName, data.weather[0].description);
weatherData.temperature = data.main.temp;
updateWeather(weatherData);
function Weather(cityName, description) {
this.cityName = cityName
this.description = descriptions;
this._temperature = '';
}
function updateWeather(weatherData) {
weatherCity.textContent = weatherData.cityName;
weatherDescription.textContent = weatherData.descriptions;
weatherTemperature.textContent = weatherData.temperature;
loadingText.style.display = 'none';
weather.style.display = 'block';
}
i have no clue what to do right now. am i not getting a value back? can someone fix it so i can understand. I'm new to this so this might be a stupid question. trying to do it the first way because i have more stuff i want to add like the weather pressure, wind speed, sunrise, etc.
You have a typo in the below code block -
function Weather(cityName, description) {
this.cityName = cityName
this.description = descriptions;
this._temperature = '';
Please correct this.description = descriptions to this.description = description
here
function Weather(cityName, description) {
this.cityName = cityName
this.description = descriptions; // <--
this._temperature = '';
}
you wrote descriptions instead of description
Error : undefined error in console, while trying to print age function
var Person = function( myName, myProfession, myage ){
this.name = myName; // Public Variable
this.profession = myProfession;
var age = myage; // Private Variable
this.myAge = function(){ // Privilaged Method
return this.age;
};
};
var syed = new Person('syed azam','developer',20);
console.log(syed + "works fine");
console.log(syed.myAge());
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
What is this.age? You did not encapsulate it correctly:
this.myAge = function(){
return myage;
};
Note that you don't have to use var age = myage;. DEMO.
I'm making a digital library with three classes: Library, Shelf & Book. Shelves have their contents as an array of books. Books have two methods, enshelf and unshelf. When a book gets unshelfed it's supposed to set delete the instance of itself from the shelf it's on and then set it's location property to null. How can I modify the shelf it's sitting on? In the constructor if I change this.location, it will just give that property a new value instead of modifying the variable it points to. I feel like this is really simple and I'm overlooking something super basic.
var _ = require('lodash');
//books
var oldMan = new Book("Old Man and the Sea", "Ernest Hemingway", 0684801221);
var grapes = new Book("The Grapes of Wrath", "John Steinbeck", 0241952476);
var diamondAge = new Book("The Diamond Age", "Neal Stephenson", 0324249248);
//shelves
var shelf0 = new Shelf(0);
var shelf1 = new Shelf(1);
//libraries
var myLibrary = new Library([shelf0, shelf1], "123 Fake Street");
//these need to accept an unlimited amount of each
function Library(shelves, address) {
this.shelves = shelves; //shelves is an array
this.address = address;
this.getAllBooks = function() {
console.log("Here are all the books in the library: ");
for (var i = 0; i < this.shelves.length; i++) {
console.log("Shelf number " + i + ": ");
for (var j = 0; j < this.shelves[i].contents.length; j++) {
console.log(this.shelves[i].contents[j].name);
}
}
}
}
function Shelf(id) {
this.id = id;
this.contents = [];
}
function Book(name, author, isbn) {
this.name = name;
this.author = author;
this.isbn = isbn;
this.location = null;
this.enshelf = function(newLocation) {
this.location = newLocation;
newLocation.contents.push(this);
}
this.unshelf = function() {
_.without(this.location, this.name); //this doesn't work
this.location = null;
}
}
console.log("Welcome to Digital Library 0.1!");
oldMan.enshelf(shelf1);
myLibrary.getAllBooks();
oldMan.unshelf();
myLibrary.getAllBooks();
Small issue with your unshelf method, easily remedied:
this.unshelf = function() {
this.location.contents =
_.without(this.location.contents, this);
this.location = null;
}
Consider, however, that shelf and unshelf should be methods of Shelf, and not of Book. Also, if you must have this method, surround it with a guard, like so:
this.unshelf = function() {
if (this.location) {
this.location.contents =
_.without(this.location.contents, this);
this.location = null;
}
}
Couple of small issues:
without works on arrays and returns a copy of the array with the elements removed - the original is untouched. So you need to pass location.contents instead of just location and reassign it back to location.contents.
Also you add the whole book to the Shelf, then try to remove it by name, so it doesn't match and get removed. So just pass this to without:
this.unshelf = function() {
if (this.location) {
this.location.contents = _.without(this.location.contents, this);
this.location = null;
}
}