I'm pretty new to React and development at all, so please don't mind this "easy" question.
I have one file "CreateRandomArray.js" which will fill an array with random numbers under 10 and checks if this number already exists.
import React from 'react';
const createRandomArray = () => {
const array = [];
let indexer = 0;
do {
let randomnumber = Math.floor(Math.random()*30)
if ( randomnumber < 10){
let result = array.includes(randomnumber)
console.log(result)
if (!result) {
array[indexer] = randomnumber
indexer = indexer + 1
} null
} null
} while (indexer < 10)
}
export default createRandomArray
Now I would love to use this array in another file but I can't find a way to get access to the value in this array. I already did several versions with "return" but it never worked.
So, does anyone knows how I can make use of this array in another .js file and assign those values for example to another array?
Thank you :)
ps: If you could explain it like you would explain it to an ape, it would be much appreciated ^^
thanks for your feedback. With the help of a colleague, I found a different solution which is a little embarrassing to post, because of its simplicity :)
This is the file which generates my random array and where I added "return array":
import React from 'react';
const createRandomArray = () => {
const array = [];
let indexer = 0;
do {
let randomnumber = Math.floor(Math.random()*30)
if ( randomnumber < 10){
let result = array.includes(randomnumber)
console.log(result)
if (!result) {
array[indexer] = randomnumber
indexer = indexer + 1
} null
} null
} while (indexer < 10)
return array
}
export default createRandomArray;
In my App.js file, I import the other one and set my variable equal to the function.
import React, { Component } from 'react';
import CreateRandomArray from './CreateRandomArray';
class app extends Component {
whatever = CreateRandomArray();
render() {
return (
<div>{this.whatever}</div>
)
}
}
export default app