Related
I want to change it with as little code as possible.
The code below can change the color, but these are long-winded.
import { createTheme } from "#mui/material/styles";
const shadowKeyUmbraOpacity = 0.05;
const shadowKeyPenumbraOpacity = 0.035;
const shadowAmbientShadowOpacity = 0.03;
function createShadow(...px: number[]) {
return [
`${px[0]}px ${px[1]}px ${px[2]}px ${px[3]}px rgba(0,0,0,${shadowKeyUmbraOpacity})`,
`${px[4]}px ${px[5]}px ${px[6]}px ${px[7]}px rgba(0,0,0,${shadowKeyPenumbraOpacity})`,
`${px[8]}px ${px[9]}px ${px[10]}px ${px[11]}px rgba(0,0,0,${shadowAmbientShadowOpacity})`,
].join(',');
}
export const theme = createTheme({
// Values from https://github.com/material-components/material-components-web/blob/be8747f94574669cb5e7add1a7c54fa41a89cec7/packages/mdc-elevation/_variables.scss
shadows: [
'none',
createShadow(0, 2, 1, -1, 0, 1, 1, 0, 0, 1, 3, 0),
createShadow(0, 3, 1, -2, 0, 2, 2, 0, 0, 1, 5, 0),
createShadow(0, 3, 3, -2, 0, 3, 4, 0, 0, 1, 8, 0),
createShadow(0, 2, 4, -1, 0, 4, 5, 0, 0, 1, 10, 0),
createShadow(0, 3, 5, -1, 0, 5, 8, 0, 0, 1, 14, 0),
createShadow(0, 3, 5, -1, 0, 6, 10, 0, 0, 1, 18, 0),
createShadow(0, 4, 5, -2, 0, 7, 10, 1, 0, 2, 16, 1),
createShadow(0, 5, 5, -3, 0, 8, 10, 1, 0, 3, 14, 2),
createShadow(0, 5, 6, -3, 0, 9, 12, 1, 0, 3, 16, 2),
createShadow(0, 6, 6, -3, 0, 10, 14, 1, 0, 4, 18, 3),
createShadow(0, 6, 7, -4, 0, 11, 15, 1, 0, 4, 20, 3),
createShadow(0, 7, 8, -4, 0, 12, 17, 2, 0, 5, 22, 4),
createShadow(0, 7, 8, -4, 0, 13, 19, 2, 0, 5, 24, 4),
createShadow(0, 7, 9, -4, 0, 14, 21, 2, 0, 5, 26, 4),
createShadow(0, 8, 9, -5, 0, 15, 22, 2, 0, 6, 28, 5),
createShadow(0, 8, 10, -5, 0, 16, 24, 2, 0, 6, 30, 5),
createShadow(0, 8, 11, -5, 0, 17, 26, 2, 0, 6, 32, 5),
createShadow(0, 9, 11, -5, 0, 18, 28, 2, 0, 7, 34, 6),
createShadow(0, 9, 12, -6, 0, 19, 29, 2, 0, 7, 36, 6),
createShadow(0, 10, 13, -6, 0, 20, 31, 3, 0, 8, 38, 7),
createShadow(0, 10, 13, -6, 0, 21, 33, 3, 0, 8, 40, 7),
createShadow(0, 10, 14, -6, 0, 22, 35, 3, 0, 8, 42, 7),
createShadow(0, 11, 14, -7, 0, 23, 36, 3, 0, 9, 44, 8),
createShadow(0, 11, 15, -7, 0, 24, 38, 3, 0, 9, 46, 8),
],
});
createShadow function and others referred to the link below.
https://github.com/mui/material-ui/blob/master/packages/mui-material/src/styles/shadows.js
So, I want to know how to change it more wisely.
I am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}
I would like to know how to reshape a 2-D linearized Array in Scala without loops, i.e. by using functional programming.
Assume a 2-D Array of 6x12 = 72 elements. What I need is to resize this Array so that it has 8x16 = 128 elements, assuming that the new elements are initialized to zero.
This is how I do it with loops:
val a = (1 to 72).toArray
a: Array[Int] = Array(1, 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)
scala> val h = 6
h: Int = 6
scala> val w = 12
w: Int = 12
scala> val H = 8
H: Int = 8
scala> val W = 16
W: Int = 16
scala> val b = Array.ofDim[Int](H * W)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for (i <- 0 until h)
for (j <- 0 until w)
b(i * W + j) = a(i * w + j)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The question is: how to do this by using functional programming?
Thank you very much!
a.grouped(w).map(_.padTo(W, 0)).padTo(H, Array.fill(W)(0)).flatten.toArray
Result (and string copied from your code, for comparison):
result : Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
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 5 years ago.
Improve this question
I'm digging through a lexer built with flex (here to be precise) and am trying to understand what is happening. Never having looked at C, I've given up trying to find the equivalent to understand parsers made with Bison, so I'm hoping to just get some basic info here, starting with what do the lookup tables in lex.yy.c contain:
static yyconst short int yy_accept[33] =
{ 0,
0, 0, 15, 13, 12, 10, 7, 13, 13, 8,
2, 9, 13, 3, 4, 0, 11, 0, 0, 2,
1, 0, 0, 0, 0, 0, 0, 0, 0, 5,
6, 0
} ;
static yyconst int yy_ec[256] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 4, 1, 5, 1, 6, 1, 1, 1,
1, 7, 1, 1, 1, 1, 1, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 9, 1, 1,
1, 1, 1, 10, 11, 8, 8, 8, 12, 8,
13, 8, 14, 8, 8, 8, 8, 15, 16, 8,
8, 17, 18, 8, 8, 8, 8, 8, 8, 8,
1, 1, 1, 1, 8, 1, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 19, 1, 20, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1
} ;
static yyconst int yy_meta[21] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 2, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 1, 1
} ;
static yyconst short int yy_base[36] =
{ 0,
0, 0, 39, 40, 40, 40, 40, 35, 10, 40,
0, 40, 0, 40, 40, 34, 40, 18, 22, 0,
0, 16, 18, 18, 15, 17, 12, 13, 15, 40,
40, 40, 24, 21, 20
} ;
static yyconst short int yy_def[36] =
{ 0,
32, 1, 32, 32, 32, 32, 32, 33, 32, 32,
34, 32, 35, 32, 32, 33, 32, 32, 32, 34,
35, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 0, 32, 32, 32
} ;
static yyconst short int yy_nxt[61] =
{ 0,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
11, 11, 11, 11, 11, 11, 11, 11, 14, 15,
18, 21, 20, 19, 16, 16, 31, 30, 29, 28,
27, 26, 25, 24, 23, 22, 17, 17, 32, 3,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32
} ;
static yyconst short int yy_chk[61] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9, 35, 34, 9, 33, 33, 29, 28, 27, 26,
25, 24, 23, 22, 19, 18, 16, 8, 3, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32
} ;
Any reference to a good explanation of a generated lex.yy.c is of course also welcome. Thanks!
(Compiling the comments to an answer, only to make a Q/A pair).
These are data to describe the "(finite) automaton" or "finite state machine" used to implement the text analysis described by the lexer generation-input/config files.
Read about "finite automata", for example this: http://www.cs.man.ac.uk/~pjj/cs211/ho/node6.html
Generally they describe the behavior of a finite state machine with which the generated lexer function will process input. You might get some clues by running Flex with the -v / --verbose option and comparing the results to the tables.
The precise meaning of the tables varies depending on the table compression type (see option -C).
I (John Bollinger, but probably agreed by most) doubt anyone can tell you specifically what these tables mean without studying (or already knowing) the Flex source. It is more or less the core idea of a generated lexer to protect the user from needing to know these details.
I am writing code where five random numbers are given a value. Those five values are then sorted in an array in numerical order. However, when one value is equaled to 1, I want that number to be in the middle of the sorted array.
When there is no number 1, the sorted array will work correctly.
let fourthRandomNumber:Int = Int(arc4random_uniform(52))
let fifthRandomNumber:Int = Int(arc4random_uniform(52))
let sixthRandomNumber:Int = Int(arc4random_uniform(52))
let seventhRandomNumber:Int = Int(arc4random_uniform(52))
let eighthRandomNumber:Int = Int(arc4random_uniform(52))
var loFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var hiFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
let loFourthRandomNumber:Int = loFourthRandomNumberOutput [fourthRandomNumber]
let loFifthRandomNumber:Int = loFifthRandomNumberOutput [fifthRandomNumber]
let loSixthRandomNumber:Int = loSixthRandomNumberOutput [sixthRandomNumber]
let loSeventhRandomNumber:Int = loSeventhRandomNumberOutput [seventhRandomNumber]
let loEighthRandomNumber:Int = loEighthRandomNumberOutput [eighthRandomNumber]
let hiFourthRandomNumber:Int = hiFourthRandomNumberOutput [fourthRandomNumber]
let hiFifthRandomNumber:Int = hiFifthRandomNumberOutput [fifthRandomNumber]
let hiSixthRandomNumber:Int = hiSixthRandomNumberOutput [sixthRandomNumber]
let hiSeventhRandomNumber:Int = hiSeventhRandomNumberOutput [seventhRandomNumber]
let hiEighthRandomNumber:Int = hiEighthRandomNumberOutput [eighthRandomNumber]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var loDealerCardsArray = [loFourthRandomNumberOutput [fourthRandomNumber], loFifthRandomNumberOutput [fifthRandomNumber], loSixthRandomNumberOutput [sixthRandomNumber], loSeventhRandomNumberOutput [seventhRandomNumber], loEighthRandomNumberOutput [eighthRandomNumber]]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var hiDealerCardsArray = [hiFourthRandomNumberOutput [fourthRandomNumber], hiFifthRandomNumberOutput [fifthRandomNumber], hiSixthRandomNumberOutput [sixthRandomNumber], hiSeventhRandomNumberOutput [seventhRandomNumber], hiEighthRandomNumberOutput [eighthRandomNumber]]
loDealerCardsArray.sort()
loDealerCardsArray[0]
loDealerCardsArray[1]
loDealerCardsArray[2]
print (loDealerCardsArray[0] + loDealerCardsArray[1] + loDealerCardsArray[2])
//NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
hiDealerCardsArray.sort()
hiDealerCardsArray[2]
hiDealerCardsArray[3]
hiDealerCardsArray[4]
print (hiDealerCardsArray[2] + hiDealerCardsArray[3] + hiDealerCardsArray[4])
////NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
var DealerCardsArray = [fourthRandomNumber, fifthRandomNumber, sixthRandomNumber, seventhRandomNumber, eighthRandomNumber]
DealerCardsArray.sort()
DealerCardsArray[0]
DealerCardsArray[1]
DealerCardsArray[2]
DealerCardsArray[3]
DealerCardsArray[4]
For example, five cards have a value of [0, 1, 2, 9, 10], and the array is sorted correctly. I am trying to sort the array so that the 1 is ALWAYS in the middle.
When an ace (value of 1 or 11) appears in the sorted array I want it to look as follows: [0, 2, 1, 9, 10]
Thank you in advance for your assistance
Something like:
var cards = [0, 1, 2, 9, 10]
// Get a list of all the aces in the array.
let aces = cards.filter { $0 == 1 || $0 == 11 }
// Now take the aces out of the array.
cards = cards.filter { $0 != 1 && $0 != 11 }
// Sort the array (unnecessary if its definitely already sorted).
cards = cards.sort()
// Re-insert the aces into the middle.
aces.forEach { cards.insert($0, atIndex: cards.count/2) }
// [0, 2, 1, 9, 10]
Might do the trick (haha)!
var dealerCardsArray = [0, 1, 2, 9, 10]
dealerCardsArray.sort()
// Only if the value 1 exists in the array, remove it and add it back in the center position
if let indexOfOne = dealerCardsArray.index(of: 1) {
dealerCardsArray.remove(at: indexOfOne)
dealerCardsArray.insert(1, at: array.count/2)
}
print(dealerCardsArray) // [0,2,1,9,10]