|
|
Is there a way to use toUpperCase and length on an array of 20 elements without typing each element out in an array?
|
|
|
|
|
What are you trying to accomplish?
|
|
|
|
|
Sure, like this
> var array = ["Eeny", "meeny", "miny", "moe"];
> array.forEach((i) => { console.log(`${i.toUpperCase()} ${i.length}`); });
EENY 4
MEENY 5
MINY 4
MOE 3
Using ES6 for brevity.
modified 6-Apr-21 21:01pm.
|
|
|
|
|
I'm a student and have an assignment where we are supposed to prompt for 20 names and then use an Array to output the names. I was wondering if there is a shorter way to write the code for the 20 name prompts? Right now I am writing a var statement for each name prompt. And then how do I get the names in the Array?
|
|
|
|
|
|
Have they not covered for loops yet?
var myArray = [];
for(var i = 0;i < 20;i++)
{
myArray[i] = NameGettingFunction();
}
This is just about the most basic iterative construct, and it will serve you always.
Or you can have 20 different prompt vars. The point of most CS classes is to get you to think about possible solutions. I get that the toolbox is a little empty right now, but learning some basic principles like iteration and recursion will get you much closer to the place that you need to be.
|
|
|
|
|
No we haven't gotten to loops yet. He said we were going to go back to this assignment later for loops. Thanks
|
|
|
|
|
That works for the prompts, but how do I get the names to output? Right now I am getting numbers with document.write.
|
|
|
|
|
Exactly the same way, but with your output instead.
|
|
|
|
|
Hi all,
I have a website form and it pops up a certain alert when the user enters some specific value in a text box. Now, I want to quantify those alerts, set up a counter and store that value in a SQL db table. Any ideas/examples on this would be much appreciated. Thanks!
|
|
|
|
|
You'll need to modify the function that pops up the alert to also call a handler of some sort, using an AJAX call, on your server to do this. SO you'll require some kind of server-side trechnology, whether it be .NET, php or whatever.
|
|
|
|
|
Hello Team
I will try with the help of colorize but it not work properly.
My basic problem is that i want to do change color of image when user select color tone tab then color tab will open if user click on a particular color on this tab the image color will change please help me
Thanks & Regards
Rajiv Rahi
|
|
|
|
|
Hi,
i am trying to use pyQT and python to get the dynamic content from a web page. The problem is that i still only get the static content. What could be wrong with the code below?
Code is based on this link: https://impythonist.wordpress.com/2015/01/06/ultimate-guide-for-scraping-javascript-rendered-web-pages/[^]
import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
from lxml.html import fromstring, tostring, iterlinks
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
print("inside 1")
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
print("inside 2")
url = 'http://www.somepage.com'
r = Render(url)
print("inside 3")
print("Sleeping..")
time.sleep(5)
print("Sleeping done")
result = r.mainFrame().toHtml()
print(result.encode('utf-8'))
I added the sleep(5) to ensure that the dynamic content has time to load but is does not help.
Why doesn't the r.mainFrame() contain the valid dynamically created page contents? Is it not updated after the pageloaded event?
Regards
|
|
|
|
|
|
Hi all, I am trying to call to a web service in a function but I get 2 errors that I cannot figure out the cause.
The first error is "Uncaught ReferenceError: distance is not defined" and the second error is "Uncaught TypeError: callback is not a function".
The following is my code snippet
function GetClosestRestaurant(location, distance, results, callback) {
_lat = location.lat();
_lng = location.lng();
var jdata = { lat: _lat, lng: _lng, distance: distance, results:results };
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState < 4) {
return;
}
if (xhr.status !== 200) {
return;
}
};
xhr.open('Get', 'http://http://localhost:21311/myService.asmx/GetClosestPlace', true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(jdata));
callback(xhr.responseText);
}
GetClosestRestaurant(location, distance, results, DisplayPlace);
function GetAddress(addr, callback) {
var location = "Some Location";
var distance = "5";
var results = "10";
callback(location, distance, results);
}
function GetLatLong() {
var address = "123 SomeStreet, SomeCity, SomeState";
GetAddress(address, GetClosestRestaurant)
}
I made sure that the variables location, distance, and results all have values by doing alert on all of them inside of the GetClosestRestaurant() function definition.
when I call GetClosestRestaurant() however, I get the error as stated above. Any help is greatly appreciated, thanks in advance.
modified 15-Sep-15 14:48pm.
|
|
|
|
|
MadDashCoder wrote: when I call GetClosestPlace() however, I get the error as stated above.
Well, there's clearly an error in your GetClosestPlace function. Since you haven't shown that function, we can't tell you what the error is.
The code you have posted has several problems:
xhr.open('Get', 'http://http://...
- double "http://" in the URL.GetClosestRestaurant(location, distance, results, DisplayPlace);
- location , distance , results and DisplayPlace are not defined at the point where you are calling this function.GetLatLong calls GetAddress , passing GetClosestRestaurant as the callback parameter. GetAddress then passes three parameters to the callback function, but your GetClosestRestaurant function requires four parameters.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi, thanks for replying. I have made changes to my code based on your suggestion and decided to use global variables as shown below.
_lat, _lng, _location, distance, results
function GetClosestRestaurant(_location, distance, results, callback) {
_lat = _location.lat();
_lng = _location.lng();
var jdata = { lat: _lat, lng: _lng, distance: distance, results:results };
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState < 4) {
return;
}
if (xhr.status !== 200) {
return;
}
};
xhr.open('Get', "http://localhost:21311/myService.asmx/GetClosestPlace", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(jdata));
callback(xhr.responseText);
}
GetClosestRestaurant(_location, distance, results, DisplayClosestRestaurant);
function GetAddress(addr, callback) {
location = "Some Location";
distance = "5";
results = "10";
callback(location, distance, results);
}
function GetLatLong() {
var address = "123 SomeStreet, SomeCity, SomeState";
GetAddress(address, GetClosestRestaurant)
}
Now I am getting the error "Uncaught TypeError: Cannot read property 'lat' of undefined" inside of the GetClosestRestaurant() function. I can't figure out why I am getting this error, since I get the value that I'm supposed to get when I do an alert passing in _lat. The other error I'm getting is "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".
I'm not sure if it's related to the first error since I made sure the variable names passed to the web service match the names of the parameters inside the web service function being called.
Thanks again for your help.
modified 15-Sep-15 16:41pm.
|
|
|
|
|
Making the variables global is a waste of time if you do not initialise them with values before calling the GetClosestRestaurant method.
|
|
|
|
|
Hi thanks for your reply,
All of the global variables are initialized prior to calling the GetClosestrestaurant function in my project I just forgot to take off the var keywords in the code above. Thanks for pointing that out.
|
|
|
|
|
Then you need to show the code that does it. What you have shown is calling a function with some variables that have no values.
|
|
|
|
|
Hi Richard, The error messages I am now getting are Uncaught TypeError: Cannot read property 'lat' of undefined" and "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".
Below is my code, global variables are declared but not initialized until the functions GetAddress() and GetClosestRestaurant() are called.
var _lat, _lng, _location, _distance, _results;
function GetClosestRestaurant(ilocation, idistance, iresults, callback) {
_lat = ilocation.lat();
_lng = ilocation.lng();
var jdata = { lat: _lat, lng: _lng, distance: idistance, results:iresults };
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState < 4) {
return;
}
if (xhr.status !== 200) {
return;
}
};
xhr.open('Get', "http://localhost:21311/myService.asmx/GetClosestPlace", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(jdata));
callback(xhr.responseText);
}
function GetAddress(addr, callback) {
_location = "Some Location";
_distance = "5";
_results = "10";
callback(_location, _distance, _results, GetClosestRestaurant);
GetClosestRestaurant(_location, _distance, _results, DisplayClosestRestaurant);
}
function GetLatLong() {
var address = "123 SomeStreet, SomeCity, SomeState";
GetAddress(address, GetClosestRestaurant)
}
The global variables _location, distance, and results are initialized in the function GetAddress(). Then the global variables _lat and _lng are both initialized in the GetClosestRestaurant().
I was able to show that all of them have a value by doing alerts so I am not seeing where I went wrong. Please point what I did incorrectly.
modified 16-Sep-15 15:42pm.
|
|
|
|
|
You are calling GetClosestRestaurant before you initialise any of the variables.
|
|
|
|
|
Hi Richard, thanks for replying. I am confused by your statement that I did not initialize the global variables before calling the GetClosestRestaurant() function because they are initialized. The way my app works is, the user enters his input into the textboxes on the page.
Then when he clicks the Submit button on the page, the function GetAddress() is called which takes the data from the textboxes and initializes the global variables with them. However, instead of passing in the values from the textboxes, I decided to hard code the values for the global variables just for testing purposes.
|
|
|
|
|
There is obviously something missing somewhere that you have not shown us. I can only suggest adding some further debug statements to trace the actual path through the code, and the values of all the variables as it goes through that path.
|
|
|
|
|