Codeacademy error on JavaScript code - javascript

Hei
I am going through the JavaScript tutorial on Codeacademy and I'm stuck on Introduction to Objects II lesson 2/30. The code that I have entered seems fine to me and the code prints the necessary line hello to the console.
But I get an error "Oops, try again. It looks like 'Hello!' wasn't logged to the console. Make sure that you properly defined the method and that you didn't change any of the provided code."
I cant seem to find anything wrong with this code that I have entered
function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function() {
console.log("Hello");
};
}
var user = new Person("Codecademy Student",false);
user.speak();

The problem is in your posted image, see the last line of the code editor:
user.speak();z //<-- z is not something what you have defined.

I went through several courses on Codeacademy. Codeacademy often has broken lessons, and if it's working on jsFiddle, it's likely two things.
1: Spelling and punctuation. Codeacademy is very specific with strings. One wrong letter, or one wrongly punctuated letter will show it as a fail.
2: Error. If this is the case, the codeacademy community usually has work arounds. If not, you can skip this particular lesson, and keep on going with the course. The 100% complete is more symbolic than anything else. As long as you're learning the concepts, it's find to skip whatever you have to.
Also, codeacademy has an excellent community that will give more specific advice tailored to the course. Here's the relevant forum for that course.
http://www.codecademy.com/forums/objects-ii/0

Related

Multiple arguments in a single parameter in jQuery/JS not working

I have the following code:
function periodClassMaker(period) {
$('.rsform-block-rsa-formaat-w' + period).parent().addClass('periodselected');
}
periodClassMaker(['1','2']);
I have also tried:
periodClassMaker('1','2');
But in both cases it fails to add the class periodselected to .rsform-block-rsa-formaat-w1 and .rsform-block-rsa-formaat-w2
And I don't understand why it fails.
This question was asked earlier but I asked it totally unrelated to my own code. As I did not know if it was at all possible. Everyone commenting me told me to put in my own code so that it was better to comment/reply on specific use cases.
Before asking this previous question I did some trial and error but did not receive an error message and also not the result I wished for. Upon which I went to SO and searched for "multiple arguments for one parameter" and similar search phrases. Many result came up and after reading about a dozen I still did not have a clear view if it was at all possible and if it was on how to do it. Therefore, I posted my own question.
I hope that after the edit the question is useful, clear and that it shows that I did a bit of research before posting.
function periodClassMaker(period) {
$(period.map(it => `.rsform-block-rsa-formaat-w${it}`).join(', ')).parent().addClass('periodselected');
}
periodClassMaker(['1','2']);
You can map the individual numbers to each selector string, and then join them by a comma for the logical OR selector.

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

Uncaught TypeError: Cannot read property 'description' of undefined

//updates display
function updateDisplay() {
displayMessage(locations [loclocal].description);
}
I have no idea what happened but i opened up my code and i started getting this error Uncaught TypeError: Cannot read property 'description' of undefined. PLease help... I was checking to see if my game worked one last time before its due and now im nervous cause its due in 12 hours for class. the rest of the code is on my git https://github.com/rileyjgr/Games the two things to look at are gameworking.html and game.js
PLease any help is appreciated idk what i did and im freaking out i cant find the error
This might be your problem:
function nextLoc(dir) {
var newLoc = nav[loclocal][dir];
if (newLoc >= 0) {
loclocal = newLoc;
} else {
displayMessage("You cannot go that way.");
}
disable_btns();
}
You check to make sure newLoc >= 0, but you don't check that it's < locations.length. Change it to this:
function nextLoc(dir) {
var newLoc = nav[loclocal][dir];
//changed following line:
if (newLoc >= 0 && newLoc < locations.length) {
loclocal = newLoc;
} else {
displayMessage("You cannot go that way.");
}
disable_btns();
}
edit: The problem (with this error) is that you are assigning the elements of the locations array to variables that have not yet been defined.
var locations = new Array();
locations[0] = loclocal_0;
will assign undefined to locations[0], because loclocal_0 is assigned later in the code.
As a quick fix, you could move
var locations = new Array();
...
locations[10] = loclocal_10;
to below:
var loclocal_10 = new rooms();
...
loclocal_10.hasItem = false
in the code. This will likely reveal other bugs, just a couple I noticed at a glance:
in function rooms(): this.toString=this.discription; contains a typo.
I recommend going through the code and adding semicolons at the end of every line where you are relying on automatic semicolon insertion currently, and indenting consistently.
Take a deep breath. This is your first year, so you're probably not expected to write good code at this point, just focus on stepping through the errors one at a time, and keep in mind that for future assignments, taking some time to learn best practices of the language you are using up front will save you from hard-to-track errors in the future.
Stackoverflow isn't really suited for debugging an entire project of this size, but I recommend that if you need personalized help (and don't have access to on-campus resources), you look into sites like instaedu, odesk, or freelancer to get someone to spend the time your program needs to get all the kinkds sorted out. One-on-one time with someone who will walk you through what you should be doing (like you might receive with instaedu) would be best for learning, whereas freelancer and odesk may be of limited usefulness if your goal is to learn how to write programs yourself.
In case anyone had the same problem. I fixed mine. I still dont know what the problem meant. but as Hart said there was some spelling mistakes (dyslexia op Am i right?) anyways i basically went through line by line and copied and pasted into a new doc and re read everything dozens of times till i found the errors. Even tho the part where i had the errror: function updateDisplay() {
displayMessage(locations [loclocal].description);
}
is exactly the same.. i still had to go through and edit everything.

Differences between MongoDB Shell Scripting and JavaScript?

Since this is for a homework assignment I do not want to post my code because it essentially gives a solution. I can post some generic snippets. I am going to start off by saying I am new to javascript and Mongo, and basically learned them in a few hours last night.
Basically I have code that when I paste into the shell, it works perfectly, but when I save it into the database and try to execute it does not work. Here is a basic example.
db.system.js.save(
{
_id: "istrue",
value: function (x){
if(x == true)
print("true");
else
print("false");
}
})
So if I copy and paste this code and set var x = true or var x = false first then it works, but if I do this:
db.eval("istrue(true);");
Then it doesn't work.
Any ideas?
I can't find any documentation on this behavior, but a little testing reveals that your code is running just fine, but either stored functions can't use print or the output of print goes somewhere other than stdout. If you return strings instead of printing them, you'll see what you would expect.
I suspect the output is going to the MongoDB logs, but I'm not sure where my install put them.

Learning jQuery: if & else statements, is this valid?

At the moment I'm learning jQuery and I hit the topic about if/else statements. As I have no background in programming this topic is something that I need to practice a bit more to get a thorough understanding of it.
The book I'm studying gave me the advice of just writing different blocks of if/else statements. I just had an idea and wanted to know if its valid:
$(morningWakeup).ready(function() {
$('#arms').reaction(function() {
if($'#kid').is(':nagging')) {
$('#kid').slap();
} else {
$('#kid').hug();
}
});
});
Let me make it clear that this is a joke of course, but I want to know if this is valid code and if you can supply me with some more examples? Thank you!
The basic form is perfectly fine, though you've misplaced some parentheses on this line: if($'#kid').is(':nagging')) {. It should be if ($('#kid').is(':nagging')) { instead. Also, note that you'll have better luck setting $('#kid').attr('behaving') to true if you just ignore() him/her for a while instead of slap()ing them. Negative reinforcement sucks. :)
You're mixing up Javascript and jQuery here: The if/else is basically valid, but the jQuery part (.is etc.) will strongly depend on whether the DOM elements exist, whether they have that property etc.
I would recommend starting with real live HTML to go along.
That, and of course the syntax error #bcat points out...

Categories