JavaScript Coding Challenge help - Quiz Question Do While Loop - javascript

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.

Related

javascript console log not working

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.")
}

Aptana Javascript how to run a single javascript program in aptana

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.

Facebook appMobi debugging when theres no distinct error

I am working on a project in appMobi, and this project has recently reached it portion that is related to facebook. However there just seems to be soo many moving parts that its virtually impossible to tell whats going on with what where and how.. more so, when everything works in the emulator but not on a device, where there is no console, no error log, nothing to work with to try and figure out the issue.
The image below is the only error I get on my device. When attempting to communicate with facebook, through appMobi specific methods. I have litteraly copied and pasted there code trying to make this work cause I know once I can see this working and how its going to work I can then start building logic around something I actually want to do. That being a mute point at the moment. Anyway, when I run this same exact code in the emulator it works exactly as expected. But running it in Test Anywhere on the device itself seems to be where this conflict is coming in.
Soo I am wondering, has anyone had this issue before out there on stack? If so what did you do to fix it? Whats the work around? Whats the means of how you debugged it and came to the conclusion as I am sure I am going to run into similar problems down the road and debugging on the device is also a bonus.
my javascript currently:
document.addEventListener("appMobi.facebook.login",function(e){
if (e.success == true)
{ console.log("Facebook Log in Successful"); }
else
{ console.log("Unsuccessful Login"); }
},false);
function fbLoginCheckz()
{
try{
AppMobi.facebook.login('user_birthday,user_about_me,user_status,offline_access,publish_stream,publish_actions,email,read_friendlists,publish_checkins,create_event');
}catch(e){
alert("Error Caught [FB 1]: "+e.message);
}
}
document.addEventListener("appMobi.facebook.logout",function(e){
if (e.success == true)
{ console.log("Logged out of Facebook"); }
else
{ console.log("Unsuccessful Logout"); }
},false);
var facebookUserID = "me"; //me = the user currently logged into Facebook
document.addEventListener("appMobi.facebook.request.response",function(e) {
console.log("Facebook User Friends Data Returned");
if (e.success == true) {
var data = e.data.data;
var outHTML = "";
for (var r=0; r< data.length; r++) {
outHTML += "<img src='http://graph.facebook.com/" + data[r]["id"]
+ "/picture' info='" + data[r]["name"] + "' />";
}
$("#blah").empty().html(outHTML);
document.removeEventListener("appMobi.facebook.request.response");
}
},false);
my html:
<br><br>
<div id="blah"></div>
RELOAD<br>
LOGIN<br>
LOGOUT<br>
FRIENDS
There was a problem with the build system and the test containers. If you build an adHoc version of your software it should work. However, all my "test anywhere" helper applications also still have the bug for the time being.
There should be an update to the test containers shortly that should fix the problem. I'll try to post back here once they have been updated.

IE stop script warning

Only in IE I get a warning when loading my site containing javascript saying that its causing the page to run slowly (and asking if I want to stop it).
I've seen other posts about this and I've looked for any long running code or infinite loops etc. The weird thing is, when I select 'No' (to not terminate the script) the page immediately loads properly. Its almost like this warning comes up right before the page is done loading. Has anybody experienced this, or know why this might be happening?
IE has its own way of making your life impossible.
Just deactivate the warning, you can further research in the future if that's necessary.
This article might help you determine why IE is giving such a warning.
Regards,
Adapted from http://www.codeproject.com/Tips/406739/Preventing-Stop-running-this-script-in-Browsers
// Magic IE<9 workaround for irritating "Stop running this script?" dialog.
RepeatOperation = function(anonymousOperation, whenToYield){
var count = 0
return function(){
if (++count >= whenToYield){
count = 0
setTimeout(function(){anonymousOperation()}, 100)
}
else anonymousOperation()
}
}
// How to implement the workaround:
//for (var i in retailers){ // Too big for IE<9! Use magic workaround:
var i = 0; var noInArray = retailers.length
var ro = new RepeatOperation(function(){
// <<Your loop body here, using return instead of continue.>>
if (++i < noInArray) ro()
else alert("Loop finished.")
}, 200) // Run this in batches of n. 500 is too much for IE<9.
ro()

Code Issues Resulting in Nothing Happening and 'send' Undefined

Here is the error I'm receiving in the console regarding 'send' undefined: https://pastebin.com/CRmZwUA4. This happens when testing our profanity filter. The word on the list is deleted and removed and the bot replies with a message stating that it was removed. This is working as it should except for the error in the console.
For the extra code not working, I'm not sure if it has to do with the error above. A few days ago the code worked fine until I made adjustments to a different section of code which was just to add in a check for the profanity filter to see if a filtered word was said by a member with a specific role. If they had the role, the filter was then ignored so they weren't blocked out. It would seem the 2 would have to do with each others but I can't figure out what the check would even affect. Again the filter worked as intended where the check was fine but the other fun code stopped working.
The code pasted below shows the fun code that stopped working. When a user besides the person being tagged says "bum" the bot replies with a fun message. That's what it's supposed to do. When any user says "bum" now, nothing happens. The concern that I have is that the testing bot used in a different server with the same code works with this perfectly fine. But the real bot on our main server is not working with the code. I even copied the file to the main bot again and restarted the bot and still nothing is happening. Sorry for the long winded post, I just want to make sure i've explained everything clearly.
if (words.includes('bum')) {
if (message.author == '<#458068171241553921>') return;
else {
setTimeout(function() {
message.channel.send('Are we talking about <#458068171241553921>?!');
}, 1500);
}
}
for (let x = 0; x < profanities.length; x++) {
if (message.member.roles.some(role => role.id === '613427562550394891')) return;
else {
if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
message.channel.send('Oooooooh you said a bad word!');
client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
message.delete();
return;
}
}
}
I figured out the issue. It was the new check that I had added. The check was to see if people had the role then it wouldn't respond. I had the check outside of the For loop which is what messed everything up. I placed the If statement inside the for loop instead so the new code is below
for (let x = 0; x < profanities.length; x++) {
if (message.member.roles.some(role => role.id === '483641589193900043')) return; {
if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
message.channel.send('Oooooooh you said a bad word!');
client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
message.delete();
return;
}
}
}

Categories

Resources