I have the following javascript code:
function vocableToTextFieldClickEventHandler(e) {
if (e.keyCode == 13) { //Enter key
if (pausedAfterAnswer) {
pausedAfterAnswer = false;
goToNextVocable();
setAnswerNeutral();
$("#vocableToTextFieldUI").val("");
return;
}
if (textIsOnlyValidCharacters(vocableToTextFieldNode.value) == false){
displayError("Not valid input!");
}
if (answerIsCorrect()) {
displayAnswerCorrect();
getActiveVocable().setPreviousAnswerStatus(1);
pausedAfterAnswer = true;
} else {
repeatList.push("x"); //This line
pausedAfterAnswer = true;
}
}
}
When adding breakpoint to the line commented "This line", it triggers no matter what e.keyCode is. I've gone through this code snippet so many times to find the error, but can't seem to find it. I was just wondering if this might be a fault in Firebug or if it's just a silly programming error. I've had some strange behaviour from Firebug earlier today that didn't make sense either, so I really don't know if I can trust it.
As I can not completely figure this one out, I can atleast conclude that some of the strange behaviour I experienced from Firebug can be fixed by going to the Firebug options and clicking "Reset all Firebug options" and then restarting the browser.
Related
I'm doing a coding challenge that requests we ask just one multiple choice question - if the answer is correct, it prints a congrats and exits the program. if wrong, it offers the user the chance to try again or exit the program. I've only been coding for a few weeks and it still simpler for me to use if/else if for this - but we need to use a Do While loop. Below is what i have so far and any advice would be appreciated:
let answer = [""]
do {
prompt("What shape is the Earth? \nA: Square\nB: Triangle\nC: Round\nD: Flat");
} while (answer != C);
console.log("I'm sorry you're an idiot.")
let retry = alert("Try again? Y/N")
if (retry == Y) {
//how to reset loop from here?
}
else if (retry == N) {
//how to exit program?
}
if(answer === C) {
console.log("Congratulations! You're not an idiot :)")
}
Let's put your code in a snippet:
let answer = [""]
do {
prompt("What shape is the Earth? \nA: Square\nB: Triangle\nC: Round\nD: Flat");
} while (answer != C);
console.log("I'm sorry you're an idiot.")
let retry = alert("Try again? Y/N")
if (retry == Y) {
//how to reset loop from here?
}
else if (retry == N) {
//how to exit program?
}
if(answer === C) {
console.log("Congratulations! You're not an idiot :)")
}
Now click the Run code snippet button above and watch what happens.
Do you see the error message? That is the first thing to fix, and this is your first step in learning how to debug your code.
Debugging is one of the most valuable skills you will need in programming. For JavaScript in a browser, you will want to get familiar with the Developer Tools that are built into every browser. If you use Chrome or the new Edge browser, here is a guide to the Chrome DevTools.
The DevTools include an interactive Console where you can see error messages and type in expressions to see what they do. They also include a Source view where you can set breakpoints in your source code and look at your variables, and many other debugging features.
A Stack Overflow snippet like the one above has a built-in Console so you can see the error message right here in the page. For code you're running in your own web page, use the browser's developer tools to see errors, test expressions, set breakpoints, and view your variables.
My problem is, is that the numpad's "/" key triggers the search bar in Firefox. I would like to prevent this from happening.
I'm currently working on an online Calculator, and ran into the problem described earlier. The reason why I would like this not to happen, is because you obviousely can't type in a division thanks to this feature. The page is only meant to run in Firefox, and it's just a little help for my little brother, so it doesnÄt have to be the most professional solution - if it works, it's fine. I've already tried the preventDefault method, but that doesn't change anything.
Any help is appreciated!
(Sorry if this question was already asked before, or it doesn't append to the rules, I'm totally new to StackOverflow)
window.addEventListener("keyup", function (event) {
let signs = ["*", "-", "/", "+"];
event.key.preventDefault;
if (!(isNaN(event.key)) || signs.includes(event.key)) output.value += event.key;
else if (event.keyCode === 8) {
output.value = output.value.slice(0, -1);
}
})
//This is a snippet of my code right now, but the event.key.preventDefault part doesn't work for my problem
I think you should use preventDefault like this:
event.preventDefault();
I don't know whether this will block the search box or not, but you can call preventDefault like this.
I am new to JS. Today I was trying to practice a little of what I've learned, and I can't get my console log to print anything. My "good morning" alert works, as well as my initial prompt question, but once I answer "yes" or "no," everything stops. Here is my code:
alert("Good morning!");
var hireMe = prompt("Are you here because you're interested in hiring me?");
if (hireMe === "yes") {
console.log("You've just made my day. Carry on.")
}
else {
console.log("Well, I hope you're at least thinking about it.")
}
Thanks.
I don't see any console window appear at all. It's as if no more code is written beyond the prompt
console.log will write to the console.
It will not cause the console window to open if it isn't already open.
You have to open your browser's console (the Developer Tools since you are using Chrome) manually.
Some browsers (IE) will crash if the developer tools (F12) are not open:
How can I use console logging in Internet Explorer?
IE unfortunately has this dependency where the console object gets created only when you open it (F12). So console.log tries to call the log method for the object console, and if that object is not yet created, it results in an error.
In chrome, sometimes the content that can be displayed on the console, can be changed to "Hide All". To view the necessary content, select the appropriate fields in the drop down.
view image
Take a look at your ' if ' statement. It does work with three but it's better to use double equals. Your code will be:
alert("Good morning!");
var hireMe = prompt("Are you here because you're interested in hiring me?");
if (hireMe == "yes") {
console.log("You've just made my day. Carry on.")
}
else {
console.log("Well, I hope you're at least thinking about it.")
}
Take a look at your ' if ' statement. It does work with three but it's better to use double equals. Your code will be:
if (hireMe == "yes") {
console.log("You've just made my day. Carry on.")
}
else {
console.log("Well, I hope you're at least thinking about it.")
}
all I would like to do is run this simple program in Aptana to see if it works, but it doesnt even show up.
I used the Javascript Template from Aptana's selection. here is the program.
<script type = "text/javascript" >
function verse (verseNum) {
var lines = "";
if (verseNum ==1 ) {
lines = "One thing I dont know why...";
} else if (verseNum == 1 {
lines = "All I know time is a valuable thing...";
} else {
lines = "NoLyrics";
}
return (lines);
}
alert (verse(1));
alert (verse(2));
</script>
When I run the program, it shows a blank webpage. I am sure it is a very simple mistake, probably completely wrong format. Any help is appreciated.
You are missing a parenthesis, added here:
} else if (verseNum == 1) {
You need to use your browser's console to discover such errors, although I'm surprised that Aptana doesn't indicate it as well.
I've written a userscript for Gmail : Pimp.my.Gmail & i'd like it to be compatible with Google Chrome too.
Now i have tried a couple of things, to the best of my Javascript knowledge (which is very weak) & have been successful up-to a certain extent, though im not sure if it's the right way.
Here's what i tried, to make it work in Chrome:
The very first thing i found is that contentWindow.document doesn't work in chrome, so i tried contentDocument, which works.
BUT i noticed one thing, checking the console messages in Firefox and Chrome, i saw that the script gets executed multiple times in Firefox whereas in Chrome it just executes once!
So i had to abandon the window.addEventListener('load', init, false); line and replace it with window.setTimeout(init, 5000); and i'm not sure if this is a good idea.
The other thing i tried is keeping the window.addEventListener('load', init, false); line and using window.setTimeout(init, 1000); inside init() in case the canvasframe is not found.
So please do lemme know what would be the best way to make this script cross-browser compatible.
Oh and im all ears for making this script better/efficient code wise (which im sure is possible)
edit: no help...? :'(
edit 28-Apr:
i re-wrote the code a little and now it looks something like this.:
if(document.location != top.location) return;
(function() {
var interval = window.setInterval(waitforiframe, 3000);
var canvas;
function waitforiframe() {
console.log("Finding canvas frame");
canvas = document.getElementById("canvas_frame");
if (canvas && canvas.contentDocument) {
console.log("Found canvas frame");
pimpmygmail();
}
}
function pimpmygmail() {
gmail = canvas.contentDocument;
if(!gmail) return;
window.clearInterval(interval);
console.log("Lets PIMP.MY.GMAIL!!!");
......rest of the code......
})();
This works perfectly fine in Firefox, but in Chrome, it gives me a top is undefined error.
Another thing i noticed is that if i remove the first line if(document.location != top.location) return; , the waitforiframe() method keeps getting called over and over again. (ie i see the "Finding canvas frame" error in the console)
can someone tell me what does the first line do? i mean what does it achieve & why does the waitforiframe() method run forever if i remove that line??
A BIG THANK YOU TO ALL WHO HELPED! -_- meh
btw, this was all i needed at the beginning of the script:
try { if(top.location.href != window.location.href) { return; } }
catch(e) { return; }