Updating ReactJS UI with loop data one after another - reactjs
I have recently created grid in UI and I want to show the complete flow of motion for all the points given by the array. However, the UI was updated with the value in the last loop.
For ex: position = [1, 2, 3]
At the initial point '1', there is a circle
then the circle move to 2 -> then 3
Instead, moving from 1 to 3
Can anybody help me to achieve this?
My whole class is as follows,
import React, { useEffect, useState } from 'react';
import { Button } from 'reactstrap';
import { motion } from "framer-motion";
import './style.css';
import Grid from './grid'
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
idOfBegin: "1",
position: 0,
xAxis: 0,
yAxis: 0,
position: [],
arrOfObj: [100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]
};
}
start = () => {
var a = document.getElementById("3");
var codinates = a.getBoundingClientRect();
var x = codinates.left + 35;
var y = codinates.top + 5;
console.log(x, y);
this.setState({ xAxis: x });
this.setState({ yAxis: y });
}
loadPosition = async (event) => {
fetch('api/groups')
.then(response => response.json())
.then(data => {
//console.log('======== ' +data);
//groups.map(group => setPosition(group));
this.setState({ position: data });
});
}
getCellValue = async (event) => {
this.loadPosition();
this.state.position.forEach(p => {
console.log('====== ' + p);
if (p != null) {
var a = document.getElementById(p);
var codinates = a.getBoundingClientRect();
var x = codinates.left + 35;
var y = codinates.top + 5;
//console.log(x,y);
this.setState({ xAxis: x });
this.setState({ yAxis: y });
}
});
}
componentDidMount() {
this.setState({ xAxis: 45 });
this.setState({ yAxis: 905 });
}
render() {
return (
<div>
<div className="grid"><Grid arrOfObj={this.state.arrOfObj} />
<motion.div animate={{ x: this.state.xAxis, y: this.state.yAxis }}
transition={{ type: "tween", duration: 1 }}
></motion.div>
</div>
<div className="control">
<Button color="primary" onClick={() => this.start()}>Start</Button>
<Button color="primary" onClick={() => this.getCellValue()}>Roll Dice</Button>
</div>
</div>
);
}
}
export default Game;
Related
pointClick listener on stacked data
curious if there is a way to get the proper index on stacked data (such as a Waterfall diagram) from AnyChart. Using the below on a waterfall gets you the same index for anything click on in a specific stack, when really there should be two index numbers (the column index and the element index within that column). // add a listener chart.listen("pointClick", function(e){ var index = e.iterator.getIndex(); }); Thanks
You can get the element index through e.target.getIndex(). anychart.onDocumentReady(() => { let data = anychart.data.set([ ["Start", 23, 30, 21], ["Jan", 22, 22, 54], ["Feb", -46, 45, -32], ["Mar", -91, -30, -28], ["Apr", 37, -27, 36], ["May", -24, 62, -48], ["Jun", 55, 40, -29], ["Jul", 31, 33, 41], ["Aug", -25, -46, 36], ["Sep", 42, 23, 22], ["Oct", 67, -44, -40], ["Nov", -24, -31, 37], ["Dec", 51, 28, 25], ["End", { isTotal: true }, { isTotal: true }, { isTotal: true }], ]); let seriesData_1 = data.mapAs({ x: 0, value: 1 }), seriesData_2 = data.mapAs({ x: 0, value: 2 }), seriesData_3 = data.mapAs({ x: 0, value: 3 }); let chart = anychart.waterfall(); let series1 = chart.waterfall(seriesData_1), series2 = chart.waterfall(seriesData_2), series3 = chart.waterfall(seriesData_3); chart.labels().position("center"); chart.labels().fontColor("white"); chart.listen("pointClick", e => { let columnIndex = e.iterator.getIndex(), elementIndex = e.target.getIndex(); // <--- HERE console.log(`Column index: ${columnIndex}`); console.log(`Element index: ${elementIndex}`); }); chart.container("container"); chart.draw(); }); #container { width: 100%; height: 350px; } <script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-core.min.js"></script> <script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-waterfall.min.js"></script> <div id="container"></div> The example above is a modified version of what you can find there: Waterfall Chart | Basic Charts | AnyChart Documentation
Project Euler problem 11 in C isnt providing any output
I have been trying to solve Project Euler problem 11. I have rechecked my code multiple times but still, after debugging, my program is not providing any answers The question states that we need to find the greatest product possible of 4 consecutive numbers: either vertically, horizontally or diagonally of the matrix given in the code below: Here is my code. #include <stdio.h> #include <stdlib.h> int main() { int matr[20][20] = { {8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8}, {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0}, {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65}, {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91}, {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95}, {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92}, {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57}, {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40}, {4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, {4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16}, {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54}, {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48} }; int horProduct=0, verProduct=0, diagright=0, diagleft=0; int maxProduct = 0; // horizontal product for (int i = 0; i < 20; i++) { for(int j = 0; j < 17; j++) { horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3]; } if(horProduct > maxProduct) { maxProduct = horProduct; } } //vertical product for (int j = 0; j < 20; j++) { for(int i = 0; i < 17; i++) { verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j]; } if(verProduct > maxProduct) { maxProduct = verProduct; } } //diagonal right for (int i = 0; i < 17; i++) { for(int j = 0; j < 17; j++) { diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3]; } if(diagright > maxProduct) { maxProduct = diagright; } } //diagonal left for (int i = 19; i > 3; i--) { for(int j = 19; j > 3; j--) { diagleft = matr[i][j]*matr[i-1][j-1]*matr[i-2][j-2]*matr[i-3][j-3]; } if(diagleft > maxProduct) { maxProduct = diagleft; } } printf("final largest product: %d\n", maxProduct); return 0; } kindly let me know what is going wrong here. Why isn't my program able to print any output?
I did a rookie mistake. The comparison with maxProduct should be inside the second loop. #include <stdio.h> #include <stdlib.h> int main() { int matr[20][20] = { {8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8}, {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0}, {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65}, {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91}, {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95}, {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92}, {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57}, {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40}, {4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, {4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16}, {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54}, {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48} }; int horProduct=0, verProduct=0, diagright=0, diagleft=0; int maxProduct = 0; // horizontal product for (int i = 0; i < 20; i++) { for(int j = 0; j < 17; j++) { horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3]; if(horProduct > maxProduct) { maxProduct = horProduct; } } } printf("hori: %d\n", maxProduct); for (int j = 0; j < 20; j++) { for(int i = 0; i < 17; i++) { verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j]; if(verProduct > maxProduct) { maxProduct = verProduct; } } } printf("verti: %d\n", maxProduct); //diagonal right for (int i = 0; i < 17; i++) { for(int j = 0; j < 17; j++) { diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3]; if(diagright > maxProduct) { maxProduct = diagright; } } } printf("diagright: %d\n", maxProduct); //diagonal left for (int i = 0; i < 20 ; i++) { for(int j = 0; j < 17; j++) { diagleft = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3]; if(diagleft > maxProduct) { maxProduct = diagleft; } } } printf("diagleft: %d\n", maxProduct); return 0; }
SwiftUI: I want to use the information in my array but I'm unable to find the right command
I'm very new to programming and I'm having trouble with the more complex aspects. I am trying to use a data set called playerCameCards and randomly add 5 of these to playerCards. I have managed to do that but I want to display name: of the last added element but I can't figure out how to do it. I'd be grateful for some help Below is my code. import Foundation import SwiftUI class Cards: ObservableObject { #Published var playerCards = [gameCards]() #Published var playerGameCards = [ gameCards(id: 1, name: "name1", attack: 92, defence: 49, gameControl: 60, creativity: 72, legend: 4), gameCards(id: 2, name: "name2", attack: 87, defence: 40, gameControl: 65, creativity: 80, legend: 2), gameCards(id: 3, name: "name3", attack: 43, defence: 93, gameControl: 40, creativity: 45, legend: 3), gameCards(id: 4, name: "name4", attack: 88, defence: 51, gameControl: 80, creativity: 92, legend: 5), gameCards(id: 5, name: "name5", attack: 85, defence: 51, gameControl: 72, creativity: 81, legend: 3), gameCards(id: 6, name: "name6", attack: 91, defence: 38, gameControl: 72, creativity: 89, legend: 5), gameCards(id: 7, name: "name7", attack: 34, defence: 95, gameControl: 40, creativity: 50, legend: 5), gameCards(id: 8, name: "name8", attack: 86, defence: 63, gameControl: 89, creativity: 84, legend: 4), gameCards(id: 9, name: "name9", attack: 90, defence: 30, gameControl: 50, creativity: 83, legend: 5), gameCards(id: 10, name: "name10", attack: 32, defence: 92, gameControl: 42, creativity: 32, legend: 4) ] } struct gameCards: Identifiable { let id: Int let name: String let attack: Int let defence: Int let gameControl: Int let creativity: Int let legend: Int } import SwiftUI struct ContentView: View { #EnvironmentObject var cardData: Cards #State var firstTime: Bool = false #State var currentCard: String = "Back" var body: some View { ZStack{ VStack{ Spacer() Text("Lets open your 1st cards").background(Color.white).font(.title) Spacer() Below is where I want show the name e.g. "name1" from the last element added to my playerCards array. I may want to make this an image in the future and use the ID: to display the corresponding image but I'm unable to access the types in my array Text("I want to show the "name" of the element in the last array I've just used").frame(width: 200, height: 200).background(Color.blue) Spacer() Button(action:{ if self.cardData.playerCards.count <= 4{ self.cardData.playerCards.append(self.cardData.playerGameCards.randomElement()!) print("\([self.cardData.playerCards.last])") } if self.cardData.playerCards.count > 4 { self.firstTime = true } }){ if self.cardData.playerCards.count <= 4 { Image("Click").renderingMode(.original).resizable().frame(width: 100, height: 100) } else{ NavigationLink(destination: ContentView()){Image("Continue").renderingMode(.original).resizable().frame(width: 100, height: 100) } } } } } } }
This is how to embed a value in a String in Swift: if self.cardData.playerCards.last != nil { Text("I want to show the \(self.cardData.playerCards.last!.name) of the element in the last array I've just used") }
How to decrease counter in for loop?
I'm using the array shuffle function from here: http://iosdevelopertips.com/swift-code/swift-shuffle-array-type.html. On this line: for var index = array.count - 1; index > 0; index -= 1 in the code below func shuffleArray<T>( arrayparam: Array<T>) -> Array<T> { var array = arrayparam for var index = array.count - 1; index > 0; index -= 1 { // Random int from 0 to index-1 let j = Int(arc4random_uniform(UInt32(index-1))) // Swap two array elements // Notice '&' required as swap uses 'inout' parameters swap(&array[index], &array[j]) } return array } Swift throws this warning: C-style for statement is deprecated and will be removed in a future version of Swift There isn't any recommendation of what should be used here. Any ideas what should replace it?
take a look at http://bjmiller.me/post/137624096422/on-c-style-for-loops-removed-from-swift-3 only decrease by 1: for i in (0...n).reverse() { } or decrease by more steps: for i in someNum.stride(through: 0, by: -2) { } More info: there are two versions for stride: through and to. Difference is <= and >= for through, while < and > for to, depending on what you need.
func shuffle<T>(array: Array<T>) -> Array<T> { var result = array for index in array.indices.reverse() { // generate random swapIndex and add a where clause // to make sure it is not equal to index before swaping guard case let swapIndex = Int(arc4random_uniform(UInt32(array.count - index))) + index where index != swapIndex else { continue } swap(&result[index], &result[swapIndex]) } return result } var arrInt = Array(1...100) shuffle(arrInt) // [28, 19, 25, 53, 35, 60, 14, 62, 34, 15, 81, 50, 59, 40, 89, 30, 2, 54, 27, 9, 82, 21, 11, 67, 84, 75, 44, 97, 66, 83, 36, 20, 26, 1, 76, 77, 8, 13, 72, 65, 64, 80, 88, 29, 98, 37, 33, 70, 52, 93, 100, 31, 4, 95, 45, 49, 61, 71, 24, 16, 12, 99, 94, 86, 46, 69, 63, 22, 48, 58, 51, 18, 43, 87, 41, 6, 92, 10, 38, 23, 68, 85, 42, 32, 55, 78, 56, 79, 3, 47, 39, 57, 90, 17, 5, 73, 7, 91, 74, 96]
for var index = array.count - 1; index > 0; index -= 1 Use a reverse range. Form the range and reverse it: for index in (1..<array.count).reverse However, as discussed in my answer here, there's a nicer way; I provide a >>> operator so you can say for index in array.count>>>1
why don't you try: for var index = array.count - 1; index > 0; index =index-1 ;
How do I iterate nested arrays in a React component?
I have a multidimensional array: an array results containing 18 arrays row, each containing 6 numbers. I want to render this as a table. The logic would be results.each as (row) <tr> row.each as (number) <td>number</td> </tr> But I can't figure out how you'd write this in JSX. const Resultset = props => ( {props.rows.map(rows => { <tr> {rows.map(number => <td>{number}</td>)} </tr> })} ); But that's not right. What's the procedure for this, how do you nest the map calls and interpolations?
One way to do it var arr = [ [ 2, 3, 4, 5, 6, 7 ], [ 8, 9, 10, 11, 12, 13 ], [ 14, 15, 16, 17, 18, 19 ], [ 20, 21, 22, 23, 24, 25 ], [ 26, 27, 28, 29, 30, 31 ], [ 32, 33, 34, 35, 36, 37 ], [ 38, 39, 40, 41, 42, 43 ], [ 44, 45, 46, 47, 48, 49 ], [ 50, 51, 52, 53, 54, 55 ], [ 56, 57, 58, 59, 60, 61 ], [ 62, 63, 64, 65, 66, 67 ], [ 68, 69, 70, 71, 72, 73 ], [ 74, 75, 76, 77, 78, 79 ], [ 80, 81, 82, 83, 84, 85 ], [ 86, 87, 88, 89, 90, 91 ], [ 92, 93, 94, 95, 96, 97 ], [ 98, 99, 100, 101, 102, 103 ], [ 104, 105, 106, 107, 108, 109 ] ]; var Hello = React.createClass({ tablerows: function() { return this.props.arr.map(rows => { var row = rows.map(cell => <td>{cell}</td>); return <tr>{row}</tr>; }); }, render: function() { return <table>{this.tablerows()}</table>; } }); ReactDOM.render( <Hello arr={arr} />, document.getElementById('container') ); In action: https://jsfiddle.net/69z2wepo/30476/
I would suggest separating the components to Resultset, NumberComponent and try to be consistent with the arrow functions. // Explicit return const NumberComponent = props => { return ( <td>{ props.number }</td> ) } const Resultset = props => { return ( <tr> { props.rows.map( number => <NumberComponent number={number} />) } </tr> ) } // Implicit return const NumberComponent = props => (<td>{ props.number }</td>); const Resultset = props => ( <tr> { props.rows.map( number => <NumberComponent number={number} />) } </tr> );
This worked well for me to map an array of arrays, I have named the array that contains arrays root, I set up to functions as so: //root render function renderNested = (row, i) => { return ( root.map(function (row, i) { return ( <div key={i}> {this.renderNested(row, i)} </div> ) }, this) ) } //nested render function renderNested = (row, i) => { return ( row.map(function (innerrow, ii) { return( <span key={ii}> Nested content here </span> ) }, this) ) }
You can iterate the first part of the array and in the second iteration you can call another component and pass the second array as a prop const Resultset = props => ( {props.rows.map(rows => { <tr> <AnotherComponent rows={rows} /> </tr> })} ); In another component you repeat the same map proccess.