-1

I am trying to print individual values from an array but it doesn't seem to work (get undefined). When i print out the array itself it shows that values are contained in the array at index 0 and 1.

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = "s994320";
if (/^[ms]\d+$/.test(testString) == true) {
    console.log(imgDim); //works
    console.log(width, height); //undefined undefined
}
Ish Sah.
  • 43
  • 3

3 Answers3

2

It is error at let testString = s994320;

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = 's994320';
if (/^[ms]\d+$/.test(testString) == true) {
  console.log(imgDim); //works
  console.log(width, height); //undefined undefined
}

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = 's994320';
if (/^[ms]\d+$/.test(testString) == true) {
  console.log(imgDim); //works
  console.log(width, height); //undefined undefined
}
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
1

by adding the quotes to s994320 the code works :

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = "s994320";
if (/^[ms]\d+$/.test(testString) == true) {
  console.log(imgDim); //works
  console.log(width, height); //undefined undefined
}
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
0

const imgDim = [];

imgDim.push(300);
imgDim.push(300);

const [
  width,
  height,
] = imgDim;

const testString = 's994320';

if (/^[ms]\d+$/.test(testString)) {
  console.log(imgDim);
  
  console.log(width, height);
}
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69