JavaScript - Silly But Perplexing Question. Beginner Here.
Hi!
I am brand new at Javascript. I have finished codecademy and Duckett's javascript book and that is it. I am trying to start coding on my own, without a web interface, and I just cannot figure out how to test my js files. I'm practicing puzzles and made a simple script. It is supposed to take a string as an argument and return the last letter of every word as a "secret message." var decryptA = function(message) { var words = message.split(" ") var secretMsg = [] for (i=0; i < words.length; i++) { secretMsg.push(message[i].slice(-1)) } return secretMsg.join() }; var result1 = decryptA("laugh ride lol hall bozo") console.log(result1) // This should be "hello" var result2 = decryptA("dog polo boo sudd noob smiley ride") console.log(result2) // This should be "goodbye" I think it works, but I cannot figure out how to test it. I made an html file that loads the script: <html> <head><script type="text/javascript" src="testing.js"></script></head> <body><p>Code running...</p></body> </html> and have opened it in firefox and chrome but I just get the "code running" message and nothing else. I have also tried firefox's scratchpad, but nothing happens. I even tried just putting console.log("hello world") into scratchpad and nothing happened. I am getting endlessly frustrated at myself for being unable to run a script. Can anyone offer a concrete way to do this? Background: I am running Ubuntu 12.04 32-bit. Thank you in advance! Cecily Reply With Quote 01-08-2015, 09:33 PM #2 felgall View Profile View Forum Posts Visit Homepage Master Coder Join Date Sep 2005 Location Sydney, Australia Posts 6,745 Thanks 0 Thanked 666 Times in 655 Posts Have you opened the browser console? That's where you are writing the results from the tests. Similar TutorialsHello, This seems like it should be extremely easy, I think I am missing something fundamental however. First, I have some XML: Code: <aaaaag> <parent text="this is the parent 0"> <child text="zero" /> <child text="one" /> <child text="two" /> <child text="three" /> <child text="four" /> <child text="five" /> </parent> <parent text="this is the parent 1"> <child text="zero" /> <child text="one" /> <child text="two" /> <child text="three" /> <child text="four" /> <child text="five" /> </parent> </aaaaag> And here is a snippet of my js: Code: aaaaag = xmlDoc.getElementsByTagName("aaaaag"); var x = aaaaag[0].childNodes[1].childNodes; cCompA.innerHTML = aaaaag[0].childNodes[1].attributes.getNamedItem("text").nodeValue; // I would have expected childNodes[0] instead !! for (i=1;i<x.length;i+=2) // I would have expected: (i=0;i<x.length;i++) instead !! { cCompB.innerHTML = x[i].attributes.getNamedItem("text").nodeValue + "<br />"; } I really have two questions here, but the first may answer the second. Also it may be worth mentioning I am just trying to work with the "parent 0" for the moment. First, I seem to have hidden nodes in my XML. When accessing the first child as an object, the index is 1, the second is 3 (I have tested this by manually putting in 1,3,5,7,9 etc as the indexs) and so on, rather than starting at 0. I'm not seeing where the hidden nodes are though, and I've run my xml through a validator on W3Cschools.com. I've seen someone recommend not using attributes and instead using a <text>one</text> format, but I am really hoping this isn't the problem as my xml file is fairly long, and it would take quite a bit of effort to rewrite it. The second question is, my for loop only outputs the last child, rather than all the previous children. If anything, I would really expect the last one NOT to get output, because of the i+=2 I am having to do because of the hidden children problem. I'm guessing this is somehow related to the first problem though. Thanks in advance! I just started to learn javascript. can someone please tell me why my script below does not work? Is it not how it should be done? <html> <head> <title> Javascript Navigation </title> <script type="text/javascript"> if(yahoo.onclick()==true) { window.location = 'http://www.yahoo.com'; } </script> </head> <body> <form> <input type="button" name="yahoo" value="Click Here to go Yahoo.com"/> </form> </body> </html> Hey guys, I've come here looking for a little help. Probably for a very easy problem to you guys lol. I have a script (this is just a snippet of it): Code: $("#wgo_onlineusers_list").find(":contains('Username')").closest("li").addClass("isOMM"); It basically finds the Username value using the online user list and adds them to a separate special "currently online" list at a different spot on the homepage of the forum. My problem is that if I have a simple username, for example "Big", it adds all usernames with "Big" in it. I know all I need to do is change it so that it catches only the exact username, but I'm having trouble finding a selector (if there is one) that can do this or if it'll be a little more work to fix. Is there any simple fix that is right in front of my eyes for this or do I need to rewrite the function completely? I'm sort of just getting into JS/jQuery and this forum looks like it will be incredibly helpful for me in the future cus of it's activity. Reply With Quote 01-30-2015, 05:17 AM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,310 Thanks 82 Thanked 4,754 Times in 4,716 Posts You really need to ask this in the jQuery forum. But I'm with you: I don't see anything in the jQuery docs that supports "equals" instead of "contains" for text values. Hey guys, I am working on building a new site and was wondering if I could get some clarity on something with JS, as I am el noobo. I have the basic layout he http://www.temp3.hangnailproductions.com/ Now this is just a mock up and I plan to validate the code, make it more semantic and clean up my CSS quite a bit, but right now I just want to get an idea of what I have to do and something has caught my eye. When the page loads, it takes quite a few seconds for the javascript I have attached to populate a few areas on the screen. At the top of the page there is a random testimonial and the current date. Both are populated from this script: http://www.temp3.hangnailproductions.com/js/master.js I don't know if these areas take a few seconds because my code is horribly wrong, too large, or if this is just normal. Does the array that holds the testimonials hold too much information? I would really appreciate some help. Also, the section that says "This should be a small blurb about the link above." is going to be a snippet of text that changes when a user mouseovers a main link at the top. I think this will also be JS and included in the same "master.js" file - thoughts? Thanks very much. (And I know it is ironic that I am asking for help on a site that sells web design, but Javascript is not something I plan to push, and if need be I would hire someone to help with any in-depth JS. I thought this would just be a simple little project...) What I'm trying to test out and learn is the process of a form using the get methode to print out the entered number to another test.htm page 1st code is the form Code: <html> <head> <script type="text/javascript"> </script> </head> <body> <form action="test.htm" method="get"> <font size="1" face="tunga">Enter a number</font> <input type="text" name="number" size="2"> <input type="submit" value="Submit"> </form> </body> </html> the 2nd code is the test.htm page I was hoping the value of var = number would document.write to this page. I was hoping I could do this without using ASP of php. I can see in the URL after the second page loads that the var number dos = the numbered entered but I can not get it to display. Code: <html> <head> </head> <body> <script type="text/javascript"> document.write("The number you entered is " + number); </script> </body> </html> as you can see im a beginner and im trying to learn this. I hope its not a stupid question Thanks John Help me anyone I am completely stuck. If you do this I shall build a shrine in your honour. In the following code the 'for' loop gets data from the relevant tags of an XML file, then puts them into the 'txt' variable. It then moves onto the next record in the XML file and does the same thing. However, I don't want the contents of txt to be overwritten each time the code loops through each record. Instead I want it to add to txt, then loop around and add the new contents of txt to the previous contents of txt. I tried txt = txt + artist + title + year; but this didn't work. Can anyone explain how to do this? Please note the contents of the variables are strings of text not numbers. Code: x=xmlDoc.getElementsByTagName("CD"); i=0; function displayCD() { for (var i=0;i<x.length;i++) { artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue); txt= "Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year; } document.getElementById("showCD").innerHTML=txt; } I want to learn javascript and I found codecademy.com which seems like a great way to learn the language. Before the first lesson it says Quote: You should be comfortable with loops, if statements, functions, and objects before attempting this course. Can someone explain what these are or point me in the direction where I can find the answers? I tried searching with google, but I havent found anything that clearly states what exactly the loop, object, etc. is. This will sound little stupid, but i need to put js below (shows adres of current page) in external .js file and to show it in html document. Code: <script language="javascript" type="text/javascript"> document.write (document.location.href); </script> Ofc, i made .js document (content is code above), in html doc i entered code below between <head> tags. Code: <SCRIPT language="JavaScript" SRC="bbb.js"></SCRIPT> But i dont know what's next I tried to insert js directly in html page (and it works) but i need this js outside page (i trying to make some opera widget so that js will be injected in current web pages). And, yes, i am 100% noob I will start off by saying yes this is homework but I only want help. I'm trying to make the content show up in the read only boxes but I can't figure out where I'm going wrong. My test page can be found at http://sidewalk-cafe.com/test/maddox-hw2a.html. It's small. Any help would be appreciated. Code: function orderForm() { var formDaysStaying = document.getElementById("daysStaying").value; var formSeason = document.getElementById("season").value; var formPropertyType = document.getElementById("propertyType").value; document.getElementById("FEE").value = "$" + today.toFixed(2); document.getElementById("totalCost").value = "$" + today.toFixed(2); document.getElementById("today").value = "$" + today.toFixed(2); document.getElementById("due").value = "$" + due.toFixed(2); } function formTotalCost(form) { const FEE = 55; var totalCost = (formPropertyType * formSeason) * formDaysStaying; var today = totalCost * .10; var due = (totalCost + FEE) - today; if (document.getElementById("daysStaying").value == "" || isNaN(document.getElementById("daysStaying").value)) alert("Please provide the number of days you will be staying."); // Submit the order to the server form.submit(); } hello! i have a problem: i want to change the height of my side bar as long as the text in the center of page long, it means when I have for ex 3 news in a page the side bar which have a background color must be increase to fit the long of text(3 news). i think it will do by javascript but i'm not sure! i my question isn't clear please tell me to explane more i really need it, Hi there, im wondering if you can help me i need help in finding javascript resources to build my skills and knowledge of Jscript because im a beginner. Please help I am very new to JavaScript and I am currently following the Eloquent JavaScript book online. On one of the exercises, we are told to make a function that returns the range of the inputs in an array. I tried this code below but it doesn't seem to work. The problem seems to be in the "If" function because if I change the condition to (start < end) it seems to work. Can someone please explain to me why this is? function range (start, end) { var j = []; if (start > end) { for (i = start; i <= end; i++) { j.push(i); } } else { for (i = start; i >= end; i--) { j.push(i); } } return j; } console.log(range(10, 2)); console.log(range(2, 10)); Reply With Quote 01-26-2015, 08:49 PM #2 Labrar View Profile View Forum Posts New Coder Join Date Jun 2008 Posts 65 Thanks 0 Thanked 12 Times in 12 Posts Why you do not use something like this var range = function(start, end, step) { var range = []; var typeofStart = typeof start; var typeofEnd = typeof end; if (step === 0) { throw TypeError("Step cannot be zero."); } if (typeofStart == "undefined" || typeofEnd == "undefined") { throw TypeError("Must pass start and end arguments."); } else if (typeofStart != typeofEnd) { throw TypeError("Start and end arguments must be of same type."); } typeof step == "undefined" && (step = 1); if (end < start) { step = -step; } if (typeofStart == "number") { while (step > 0 ? end >= start : end <= start) { range.push(start); start += step; } } else if (typeofStart == "string") { if (start.length != 1 || end.length != 1) { throw TypeError("Only strings with one character are supported."); } start = start.charCodeAt(0); end = end.charCodeAt(0); while (step > 0 ? end >= start : end <= start) { range.push(String.fromCharCode(start)); start += step; } } else { throw TypeError("Only string and number types are supported"); } return range; } Thats pretty similar to php range Reply With Quote 01-26-2015, 09:25 PM #3 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,310 Thanks 82 Thanked 4,754 Times in 4,716 Posts Ignore Labrar's ridiculously complicated answer to a very simple problem. Instead look CAREFULLY at this code: Code: if (start > end) { for (i = start; i <= end; i++) { Let's use your first example, where you call range(10, 2) So start is 10. And end is 2. So you get to if ( start > end ) and JavaScript executes that as if ( 10 > 2 ) Indeed, 10 *IS* greater than 2, yes? SO you the go execute the NEXT line: for (i = start; i <= end; i++) Which, of course, JavaScript then executes as for (i = 10; i <= 2; i++) But then 10 is *IMMEDIATELY* <= 2. So the for loop NEVER RUNS. All you need to do to fix your code is change Code: if (start > end) { into Code: if (start < end) { PRESTO! ********** The tiniest bit of debugging using your JavaScript debugger would have found this. But also just learning to THINK like the computer would help. Actually run the code in your head, using your example numbers, and you'd see where you went wrong. Hey guys, Thanks for taking the time to read my post. I am not new to programming or scripting, but I am completely new to javascript and web-based code. Hopefully you guys could help me understand what is going on and maybe help me figure out what I am trying to do. What I am trying to do: -There is an element that has a value <td class="flashField" onmouseover="clearAnswer()"> <div id="flashAnswer" onmouseover="clearAnswer()">answer string here</div> </td> -There is a text box that needs to have the answer above given to it <td class="flashField"> <input type="text" tabindex="1" onkeypress="clearAnswer()" name="q" maxlength="256" size="50"> </td> -There is a next button that needs to be executed <td align="right"> <input type="image" tabindex="2" alt="Next" src="http://website.com/images/next.gif"> </td> So what I am trying to do is create a script through Greasemonkey that will automatically do these things. I am starting small and trying to just get the value of flashAnswer to appear as an alert on the screen by using this code: var answer = document.getElementById('flashAnswer'); alert(answer); But the problem is it posts some weird value ([object XrayWrapper [object HTMLDivElement]]) that most definitely is not "answer string here" and is a bit above me at this point. Any ideas? Any help would be great guys! Thanks! -ellosiph 3 questions. I am very very new to JS (i am a html/css wiz but am just now learning js. I have taken 2 university CS courses but both were with C++)... My friend sent me these problems and wanted some help. Anyone here wanna give em a go? Given the string "Frank delete from users Beans" in a textfield If you find any sql token you find (like delete, drop, truncate, etc), strip it out, and regenerate the original string without those tokens; place the regenerated string (without the sql) back in the textfield. If You cannot strip out the sql and place the sanitized value back in the textfield, you will not get full credit for this question. Write a document that contains 5 paragraphs, and 4 headers. Create a script that finds all of them and puts "FOUND IT" beside it. Create a document that lets user's enter a planet's (like Jupiter, Saturn, Mars...) name (via a select or input field), and at the click of a button (or when the selected option was chosen), have the distance (from the Sun or Earth) appear next to that select box or textfield (which ever you choose to implement for the input from the user.) Please use Google to find the distances. HINT: use innerHTML = '...' Thanks guys! Hi people I'm hoping someone can help me. I have NO knowledge of Javascript but I'm not afraid of code and happy to look at it and work out what is doing what. I need someone to provide me with a small sample of code to get me started on this task. I need a webpage with a number of drop down menus. Depending on what the user selects in these menus, depends on what sentence or paragraph is then displayed in a box. For example you might have a page dedicated to food. So the user would select egg in the first drop down and the box will change to display "you can't make much with that on it's own".# You'll then select flower in the second and it'll adjust to a paragraph maybe explaining where you could go next with that. In my page I need 6 drop down menus with about 8 choices in each menu (nothing related to food). Not sure if I would need to tie this in with a database or if I could just make do with the code on the page. As I said I'm hoping someone can just kick me off with something do I can get a rough idea of how I would go about coding it. Maybe an example contain 3 drop down menus with 2 choices on each and a few possible sentences or paragraphs that it could generate in a box. If not then any advice would be really appreciated as I said I'm a complete newbie and I've never even looked at Javascript code to my knowledge. Many thanks Hi there! Here's my problems: The problem 1 is about making triangle with "*", but i must not use <center> so the triangle must be on the left side.. Here's my code: var x = parseInt(prompt("Your value","")); var A= ""; var B=[" "]; for (i=0; i<x; i++) B[i+1]=B[i]+" "; for (i=0; i<x; i++) { A = A+"*"; document.write("<br>" + B[x-i] + A) } How can i make this code more basic? The problem 2 is about taking random different elements from Array. Here's code: var items = Array(0,1,2,3,4,5,6,7,8,9), a; document.write("Secilen elemanlar: "); for (i=0; i<5; i++) { a= Math.floor(Math.random()*items.length); document.write(items[a]); items.splice(a,1); } In that problem how can i do the same thing without splice. And the problem 3 is about number triangle like this 1 232 34543 I couldn't write any code,and cane you give some advices like "what methods i must use"? Reply With Quote 02-04-2015, 07:34 PM #2 felgall View Profile View Forum Posts Visit Homepage Master Coder Join Date Sep 2005 Location Sydney, Australia Posts 6,745 Thanks 0 Thanked 666 Times in 655 Posts splice is about the only command there that you should consider using. Yuo are misusing parseInt() where you should be using Number() and the prompt() and document.write() should never be used at all - they became obsolete many years ago. Hi all, this is my first attempt at JavaScript code. I'm trying to write code for an Adobe Acrobat form. Here is my situation: I have several boxes, all containing numbers such as 2014, 08, and 000000000. I want to concatenate them all into one long number like 080000000002014. I've looked at a ton of sample code, but nothing I try seems to work. Any help is greatly appreciated.
var suitArray = ["Hearts","Diamonds","Clubs","Spades"]; var faceArray = ["Ace", - Pastebin.com Doing a school assignment and Im totally stumped, google isnt any help nor is any of these textbooks. I need to tally up the playerhands and dealer hands and display the winner. I can get it to display the value of the current card. eg 7 of diamonds = 7 points but I cannot get it to tally up all the cards dealt. is this code right its supposed to be a code that prompts the user for a number less than 100 and outputs only odd numbers less than or equal to the number entered by the user.for example if a user eneters the number 10 you should display 1,3,5,7,9 to the browser window?...im not sure if i have done it right..i gave it a go though! var input = prompt("Enter a number less than 100:", "10"); if( input <= 100){ nbsp; for (var i = 1, i <= input, i++2){ nbsp; document.write(i", "); nbsp; } }else{ nbsp; alert(input + " is not less than 100!"); } I'm trying to get this simple (not to me) ascii animation going and firebug is giving me an error saying "document.getElementById("frameArea") is null. I've got an element named frameArea and I wonder if it's buried too deep. I don't really understand this code which I admit is a big part of my problem. I have seen what it is supposed to do, which is play a simple animation. Will someone please help me decipher what this code is saying? Code: <html> <head> <title>Animation</title> <script type="text/javaScript"> function playAnimation() { frameStr = document.getElementById("frameArea").value; if(frameStr.indexOf("\r\n") != -1) { frameSeq = frameStr.split("=====\r\n"); } else { frameSeq = frameStr.split("=====\n"); } currentFrame = 0; showNextFrame(); } function showNextFrame() { document.getElementById("displayArea").value = frameSeq[currentFrame]; currentFrame = (currentFrame+1)% frameSeq.length; timer = setTimeout("showNextFrame();", 250); } </script> </head> <body> <div style="text-align:left"> <h1>My Animation</h1> <hr /> <form name="animationForm"> <table> <tr><td align="center"><b>Enter frames below separated by "====="</b> <td> <td align="center"> <tr><td><textarea name="frameArea" rows=12 cols=35 wrap="virtual"></textarea> </td> <td align="center"> <br /> <input type="button" value="Play --->" onclick="playAnimation()"> </td> <td><textarea name="displayArea" rows=12 cols=35 wrap="virtual"></textarea> </td> </tr> </table> </form> </div> </body> </html> |