Generating id's and pushing them into an array in Javascript? [closed] - javascript

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 2 years ago.
Improve this question
I have a function generating color id's:
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color
}
And then I'm using another function, as I want to have an array of six generated colors:
const colorArray= ()=> {
let colors = []
for (let i=0; i>=6; i++ ){
colors.push(hexaColor[i])
console.log(colors)
}
return colors;
}
console.log(colorArray())
however, what I see in a console is just an empty array. What am I doing wrong? Any help appreciated.

You have a code error
The loop did not run at all
The condition will never be meti>=6
Change the condition at the top of the loop
for (let i=0; i>=6; i++ ){
This is how it works
for (let i=0; i<=6; i++ ){
also this line fix to
colors.push(hexaColor()[i])

The issue is that in your loop you said when i >= 6, but it's i <=6 because i starts from 0.
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color;
}
const colorArray = ()=> {
let colors = []
for (let i=0; i<=6; i++ ){
colors.push(hexaColor());
}
return colors;
}
console.log(colorArray())

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/

Create a function that takes a word and returns true if the word has two consecutive identical letters [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 1 year ago.
Improve this question
// Create a function that takes a word and returns true if the word has two consecutive identical letters.
What am I doing wrong?
module.exports = (word) => {
for (let i = 0; i <= word.length; i++) {
for (let j = i + 1; j <= word.length; j++) {
if (word[j] == word[i]) {
return true;
}
} return false;
}
};
You can do this with only 1 loop.
function hasConsecutiveIdenticalLetters(word){
for (let i = 1; i < word.length; i++) {
if (word[i-1] === word[i]) {
return true;
}
}
return false;
}
You can also achieve this like below using some
const hasConsecutiveIdenticalLetters = (word: string) => (word).split('').some((letter, index) => letter === word[index + 1]);

how to multiply from the starting of index [ 0 ]. Here it is taking only 4 values . [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 5 years ago.
Improve this question
When I try to execute this it takes index from [1] position but not zero. How to take from zero.
function mul(){
var temp = 1;
for (i=1 ; i < arguments.length; i++){
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2,2,2,2,2);
alert(list);
This is because var i is set to 1. arguments is an array-like object and it also start from 0 index. In your case your are starting from 1st index.
function mul() {
var temp = 1;
for (i = 0; i < arguments.length; i++) {
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
According to airbnb style it is better to use rest syntax instead of arguments object. Beside i in the for loop has not defined with var keyword. In that case it will be global variable & will be accessible from other function. So defining a variable with var keyword will bound its scope to current function only. You can also explore let
function mul(...args) {
var temp = 1;
for (var i = 0; i < args.length; i++) {
temp = temp * args[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
The line 3 for loop:
for (i=0 ; i < arguments.length; i++){...

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] = ...
}
}

For-loop is never entered [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
I have a function meant to construct an array full of objects using a for-loop. However, my debugger jumps over the for-loop for some reason, and i don't know why. here is the function:
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i === len; i++) {
arr.push({
name: i,
count: 0
});
}
return arr;
}
This line
for (var i = 2; i === len; i++) {
Should be
for (var i = 2; i <= len; i++) {
change you for loop like this. this my helps you
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i <= len; i++) { // change this line
arr.push({
name: i,
count: 0
});
}
return arr;
}

Categories