我想读取变量“ x1”中的值“ -2.5”,以及其他名为“ y1”的变量中的值“ 0.4”。与下面一行相同:在变量“ x2”中读取“ 12.1”,在变量“ y2”中读取“ 7.3”
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var x1 = parseFloat(lines[0]);
var y1 = jQuery(x1).next();
var x2 = parseFloat(lines[1]);
var y2 = jQuery(x2).next();
console.log(x1);
console.log(y1);
console.log(x2);
console.log(y2);
this is the problem i'm solving and the code i've made so far, but not accepting "Wrong Answer 85%"
未捕获的ReferenceError:未定义jQuery
答案 0 :(得分:0)
我认为您可以参考一下此解决方案,希望对您有所帮助
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
// This will convert a string into an array seperated by space (" ")
const generatePosition = string => string.split(" ");
// This will map all our initial values and change them into a full array
const pos = lines.map(string => generatePosition(string));
console.log(pos);
// From now you can freely access the variable the way you want, this is just for sample demo
const a = pos[0][0];
const b = pos[0][1];
console.log(a);
console.log(b);
答案 1 :(得分:0)
可以通过调查是否加载了Jquery来解决您的错误,但是使用当前代码无法为您提供正确的结果。
您可以这样做:
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var lineParts = lines[0].split(" ");
var x1 = parseFloat(lineParts[0]);
var y1 = parseFloat(lineParts[1]);
lineParts = lines[1].split(" ");
var x2 = parseFloat(lineParts[0]);
var y2 = parseFloat(lineParts[1]);
console.log(x1, y1, x2, y2);
答案 2 :(得分:0)
您可以重新格式化lines
数组,以便每个值都位于其自己的元素中吗?这样可以更轻松地访问每个元素。像这样:
var lines = [
Array("-2.5","0.4"),
Array("12.1","7.3")
];
然后您可以通过以下方式访问值:
var x1 = parseFloat(lines[0][0]);
var y1 = parseFloat(lines[0][1]);
var x2 = parseFloat(lines[1][0]);
var y2 = parseFloat(lines[1][1]);