I am unable to print result in line by line,it's coming in one line ,how to solve this....
I tried this code-
<script>
function know(){
var num =Number(document.getElementById('number').value);
var range=Number(document.getElementById('range').value);
var output="";
var final=[];
for(i=1;i<=range;i++)
{output=i*num;
final.push(output)
final=final.replace(",","<br/>")}
document.getElementById('result').innerHTML=final
}
</script>
There is 2 issues in it:
the for loop i you need to declare as a var or let(I recommend let)
there is no replace method in array, you can use map in this case
So you can re write the method like this.
<script>
function know(){
var num =Number(document.getElementById('number').value);
var range=Number(document.getElementById('range').value);
var output="";
var final=[];
//for(i=1;i<=range;i++)
for(let i=1;i<=range;i++)
{output=i*num;
final.push(output)
//final=final.replace(",","<br/>")
}
final = final.map((d, ind) => d*(ind+1))
document.getElementById('result').innerHTML=final
}
</script>
Related
Goal: I have a string of code that is looking to compare two parameters. If one of the parameters is not contained within the other parameter, I need to execute a script.
Good things: The script to hide everything (in the code below, everything after "if(a > 0);") works perfectly.
Issue: When I add the indexOf function to try to do the comparison, I get an error saying: Uncaught TypeError: str.indexOf is not a function
What I've tried: I initially tried the str.includes function but found through research that that function is not supported in all browsers and was directed to try doing the indexOf function. Everything I've found online seems to say that the indexOf function should work?
My code is:
<script language="javascript" type="text/javascript">
var str = [#authfield:Authentications_2_Region];
var a = str.indexOf([#field:Location_2_Region_GL]);
if(a > 0);
function hide_column(column_order)
{
var tbl = document.getElementsByTagName("table")[0];
var table_header = tbl.getElementsByTagName('th')[column_order];
table_header.style.display=stl;
var rows = tbl.getElementsByTagName('tr');
for (var row=1; row<rows.length;row++)
{
var cels = rows[row].getElementsByTagName('td');
cels[column_order].style.display=stl;
}
}
var stl='none';
hide_column(0);
hide_column(1);
hide_column(2);
hide_column(3);
hide_column(4);
hide_column(5);
hide_column(6);
</script>
I'm not good with Javascript, any help is appreciated.
Thank you #Satya - he got me in the right direction. I needed to convert the parameters to a string. Successful code is
<script language="javascript" type="text/javascript">
var str = toString([#authfield:Authentications_2_Region]);
var a = str.indexOf(toString([#field:Location_2_Region_GL]));
if(a > 0);
function hide_column(column_order)
{
var tbl = document.getElementsByTagName("table")[0];
var table_header = tbl.getElementsByTagName('th')[column_order];
table_header.style.display=stl;
var rows = tbl.getElementsByTagName('tr');
for (var row=1; row<rows.length;row++)
{
var cels = rows[row].getElementsByTagName('td');
cels[column_order].style.display=stl;
}
}
var stl='none';
hide_column(0);
hide_column(1);
hide_column(2);
hide_column(3);
hide_column(4);
hide_column(5);
hide_column(6);
</script>
I am beginner with javascript.I want to create array as 1,2,3,4,5
But I get o/p as ,1,2,3,4,5
I tried with .split() in javascript but I am not getting the required output.
var string="testrating_1testrating_2testrating_3testrating_4testrating_5";
var temp = new Array();
temp = string.split("testrating_");
for (a in temp ) {
temp[a] = temp[a];
}
fiddle
You could do it this way:
var string = "testrating_1testrating_2testrating_3testrating_4testrating_5",
temp = string.split('testrating_');
temp.shift(); //remove fist item which is empty
console.log(temp); //["1", "2", "3", "4", "5"]
The actual value of temp is ["","1","2","3","4","5"]. This is happening because of .split and the string starting with the delimiter.
If you always know the delimiter will be at the start of the string, you can do this, which removes the first element of the Array:
temp = string.split("testrating_").slice(1);
You can try this...
<!DOCTYPE html>
<body>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var string="testrating_1testrating_2testrating_3testrating_4testrating_5";
var temp = [];
temp = string.split("testrating_");
var tempNew=[];
for (a in temp ) {
if(temp[a]!==''){
tempNew.push(temp[a]);
}
}
alert(tempNew);
}
</script>
</body>
</html>
JSFiddle
You are getting ,1,2,3,4,5 because there is no character at the beginning of the string before testrating_ by which you are separating.
either you can remove testrating_ from first position like statement below
var str="1testrating_2testrating_3testrating_4testrating_5";
str.split("testrating_")
Or you can remove character from first index after getting result as from array
var str="testrating_1testrating_2testrating_3testrating_4testrating_5";
str.split("testrating_").slice(1);
Do this:
<!DOCTYPE html>
<body>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var string="testrating_1testrating_2testrating_3testrating_4testrating_5";
var temp = new Array();
temp = string.split("testrating_");
var output = new Array();
for (x = 0; x < temp.length; x++) {
output[x - 1] = temp[x];
}
alert(output);
}
</script>
</body>
</html>
You could always use .splice:
var string="testrating_1testrating_2testrating_3testrating_4testrating_5";
temp = string.split("testrating_"); // temp = ["","1","2","3","4","5"]
temp.splice(0,1) // temp = ["1","2","3","4","5"]
What splice does here is goes to the first value in the array (index #0) and removes one value from that point which would be the empty string "".
To give a little more of an explanation, you could remove the empty string and "1" by using splice like so:
temp.splice(0,2) // temp = ["2","3","4","5"]
Keep in mind that splice directly effects the array it's called upon and returns an array of values that were removed. So if you did the following:
values = temp.splice(0,2) // values=["","1"] temp=["2","3","4","5"]
Also, in this example creating a new array in the following fashion temp = new Array(); is redundant since string.split() effectively creates an array and fills it with the values that were splitted. Keep that in mind going forward. Hope this helps and welcome to the world of programming!
var string="testrating_1testrating_2testrating_3testrating_4testrating_5";
// Split and remove first entry and return an array
var final = string.split("testrating_").slice(1);
$('#out').text(JSON.stringify(final));
Try this:
var string = "testrating_1testrating_2testrating_3testrating_4testrating_5";
var temp = string.split("testrating_").join("").split("").join();
console.log(temp);
Can also be done like this:
var temp = string.replace(/testrating_/g,"").split("").join();
or just:
var string = "testrating_1testrating_2testrating_3testrating_4testrating_5";
var temp = string.match(/[\d]/g); //your regex here
console.log(temp);
Not sure about the performance though.
Hello I want to extract elements from both arrays with the same url .How can i loop these two arrays and get their content, because it gives me undefined for the news_url and i think it outputs twice the items in the console.
function geo(news_array,user_tweets){
console.log(news_array,user_tweets);
for (var x=0; x<user_tweets.length; x++) {
var user = user_tweets[x].user;
var date = user_tweets[x].date;
var profile_img = user_tweets[x].profile_img;
var text = user_tweets[x].text;
var url=user_tweets[x].url;
second(user,date,profile_img,text,url);
}
function second(user,date,profile_img,text,url){
for (var i = 0; i < news_array.length; i++) {
var news_user = news_array[i].news_user;
var news_date = news_array[i].news_date;
var news_profile_img = news_array[i].news_profile_img;
var news_text = news_array[i].news_text;
var news_url=news_array[i].url;
if (url==news_array[i].news_url) {
geocode(user,date,profile_img,text,url,news_user,news_date,news_profile_img,news_text,news_url);
}
}
}
function geocode(user,date,profile_img,text,url,news_user,news_date,news_profile_img,news_text,news_url) {
console.log(url,news_url);
}
}
The problem is
in news_tweets function, you add news_url to news_array. So you should call
news_array[i].news_url
in second function.
I modify your code as
news_url: (item.entities.urls.length > 0)?item.entities.urls[0].url : '' in news_tweets function
add close brace } for geo function and remove } from last
add new_array parameter to second function like second(user, date, profile_img, text, url,news_array);
Modify code can be tested in http://jsfiddle.net/rhjJb/7/
You have to declare some variables before the first for loop, so that they can be accessed in the scope of the second function. Try to replace your first for loop with the following code:
var user, date, profile_img, text, url;
for (var x=0; x<user_tweets.length; x++){
user = user_tweets[x].user;
date = user_tweets[x].date;
profile_img = user_tweets[x].profile_img;
text = user_tweets[x].text;
url=user_tweets[x].url;
second(user,date,profile_img,text,url);
}
Moreover, in the if of your second function, news_array[i].news_url isn't defined. Use if (url == news_url) instead.
I need to dynamically assign the name of a function to an element of an associative array. This is my attempt which does not work. The problem I am asking for help with is here where I try to call the function: cr['cmd1'](x);
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var cr =[];
var x = 5;
cr['cmd1'] ='foo';
var msg = cr['cmd1'](x);
alert(msg);
function foo(y){
return y;
}
</script>
</head>
<body>
</body>
</html>
Edit: I being passed a string here cr['cmd1'] ='foo'; that I cannot control. That is why I have to work with a string as a starting point from an external application.
Access the functions using this syntax window[function_name]('para1');
Your usage will be something like this
var msg = window[cr['cmd1']](x);
If you want to store it as a function, pass the function directly. Otherwise, if you just want to store it as a string, then you can use the quotes.
Change:
cr['cmd1'] ='foo';
To:
cr['cmd1'] = foo;
I would use window[] and make sure its a function before trying to execute it since you don't have control over what is passed.
var f = window[cr['cmd1']];
if(typeof f==='function') {
f(x);
}
What you are doing there is assigning a function to an array. A more common pattern that you are probably trying to do is to call a function on an object with the array notation.
<script type="text/javascript">
var cr = {};
cr.cmd1 = function foo(y){
return y;
};
var x = 5;
var msg = cr['cmd1'](x);
alert(msg);
</script>
This code results in an alert box that contains the number 5.
What is the logic behind the following code?
var next, output = null, thisNode;
It appears like it's some type of coalescing like var foo = bar || baz;, but I'm not so familiar with the commas.
It's just a shorter way of writing:
var next;
var output = null;
var thisNode;
multiple variable declarations.
its the same as this:
var next;
var output = null;
var thisNode;