Javascript Multiplication Table Array [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have been trying to do this for a while and I can't quite get it. I basically want a 2d array that is a multiplication table. So if I reference multTable[5][5] I would get 25. I have found scripts for a table that is printed but not one for an array. This is the best code I have so far.
var multTable;
for(var v = 0; i<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}

Javascript doesn't have built-in type of multi-dimensional array. So you can't declare multTable first and then directly use multTable[v][i]=i*v. You need to create an array of arrays.
var multTable = [];
for (var v = 0; v < 13; v++) {
multTable.push([]);
for (var i = 0; i < 13; i++) {
multTable[v].push(i * v);
}
}
multTable[5][5] // 25
Or alternatively, you can use object.
var multTable = {};
for (var v = 0; v < 13; v++) {
multTable[v] = {};
for (var i = 0; i < 13; i++) {
multTable[v][i] = i * v;
}
}
multTable[5][5] // 25

I'm not exactly sure what the question is, but it looks like your first for loop exit condition is using the wrong variable. Try the following:
var multTable;
for(var v = 0; v<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}

Related

How to give same id to dictionary if they belong to same network [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
hint
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}]
convert the above list to
output=[{'s':1,'t':2,'id':1},
{'s':2,'t':3,'id':1},
{'s':3,'t':1,'id':1},
{'s':4,'t':5,'id':2},
{'s':5,'t':6,'id':2},
{'s':6,'t':4,'id':2}]
info - we are giving the same id to the first 3 dictionaries in the list because they belong to the same network. (if you plot graph). same with the next 3 dictionaries
this code it's work:
let V;
let adjListArray=[];
let id = 1;
let ids = [];
function Graph(v)
{
V = v
for (let i = 0; i < V; i++) {
adjListArray.push([]);
}
}
// Adds an edge to an undirected graph
function addEdge(src,dest)
{
adjListArray[src].push(dest);
adjListArray[dest].push(src);
}
function DFSUtil(v,visited)
{
visited[v] = true;
ids[v] = id;
for (let x = 0; x < adjListArray[v].length; x++)
{
if (!visited[adjListArray[v][x]]){
DFSUtil(adjListArray[v][x], visited);
}
}
}
function connectedComponents()
{
let visited = new Array(V);
for(let i = 1; i < V; i++)
{
visited[i] = false;
}
for (let v = 1; v < V; ++v)
{
if (!visited[v])
{
DFSUtil(v, visited);
id++;
document.write("<br>");
}
}
}
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}];
Graph(my_dict.length + 1);
for(var i = 0 ; i < my_dict.length ; i++){
addEdge(my_dict[i].s,my_dict[i].t);
}
connectedComponents();
for(var i = 0 ; i < my_dict.length ; i++){
my_dict[i].id = ids[my_dict[i].s];
}
console.log(my_dict);
You can use the depth-first search algorithm to calculate each node belonging to any component and this is by iterating the search algorithm on the unvisited nodes.
it's be some like the code in this url:
https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/

Array in each position successively Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've got trouble with arrays. I'm trying to iterate elements for each position in the array using javascript.
so, I need to do that on the first position, be a value [1]. On the second position, [2, 3, 4]. on the third position, [5,6,7,8,9] and successively two by two.
My attempt was did a for loop:
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
console.log(`${i} ${j}`)
}
}
But while looping, the indices (i) repeat with (j).
What to do?
Try this code.
var counting = 1;
for (let i = 1; i <= 5; i = i + 2) {
var str = "";
for (let j = 1; j <= i; j++) {
str += `${counting} `;
counting++;
}
console.log(str);
}

How to add spaces to for loop output in javascript? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want to add one space in between each outcome in this for loop,
for (var i = -52; i <= 1066; i++) {
document.write(i)
}
You can add to do it
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
Or just add to space in the write
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
this should help you out.
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
or
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
Browsers are designed to ignore whitespace such as spaces, typing in multiple ' ' (spaces) will be reduced to a single space. If you're looking to add multiple spaces in Javascript the easiest way is to use &nbsp. Such tags are called entities and numerous characters can be printed using entities. More can be found here
for(var i = 0;i < n;i++)
{
document.write("&nbsp")
}
for (var i = 52; i <= 1066; i++) {
document.write(i+'\t') }
Try this:
let result = [];
for (let i = -52; i <= 1066; i++) {
result.push(i);
}
document.write(result.join(" "));
Suggestion: avoid using document.write at best, especially in a big for loop.

Find mode using Javascript with 2 rules based on my code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
so this is what I've achieved so far
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 1; i <= 10; i++)
arr[i] = 0;
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i < 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
so the scope for my case are
The sequence value is >= 1 and <=10
The sequence length is > 0
The sequence is an array of integer
Example 1
Input : sequence = [1, 10, 10]
Output : 10
Example 2
Input : sequence = [1, 3, 3, 3, 1]
Output : 3
It seems easy, I've tried to figure out but I cant find where is the mistake in my code, It's seems legit for me
As suggested by Maxim Krizhanovsky, the last loop should go from 1 to 10 instead of to sequence.length. Here is the working code
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 0; i <= 10; i++)
arr.push(0);
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i <= 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
The output is 10 and 3 respectively to your input.

A quite weird array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here is my problem, I'm programming a game and I need to use a very simple array, the problem is that I create it, everything's ok, and the next line, the values of an entire line in the array change and I don't understand why, here is my code :
var ciblesPossibles = new Array();
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 5; j++) {
ciblesPossibles[i,j] = 1 + portee(i, j, ID, pos, carte.effet.portee) + duBonCote(i, ID, carte.effet.ciblesLegales);
}
console.log('1/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
for (i = 0; i < 2; i++) {
console.log('2/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
var max = maxTab(ciblesPossibles);
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
ciblesPossibles[i,j] = Math.floor(ciblesPossibles[i,j] / max);
console.log(ciblesPossibles[i,j]);
}
}
portee() and duBonCote() are two functions which return just 1 or 0.
When I'm at the console.log('/1...'), I have something like 33222 and 22211 (it's what I want), but when I'm at the console.log('/2...'), I have 22211 and 22211 ... What can make the first line change in my array ?
Regards
Two dimensional arrays are accessed as a[i][j], not a[i,j].
The latter will be treated as a use of the comma operator, and evaluates to just a[j], i.e. a one-dimensional matrix.
You'll be wanting something more like:
var ciblesPossibles = []; // create array to hold rows - NB: not "new Array():
for (var i = 0; i < 2; i++) {
ciblesPossibles[i] = []; // create the individual row
for (var j = 0; j < 5; j++) {
ciblesPossibles[i][j] = ...
}
}

Categories