嗨,我正在尝试在JS中创建一个函数,该函数接受字符串并检查字符串中是否有相等数量的“ x”和“ o”。
到目前为止,我的代码(无法正常工作):
var vector = new Point(10, 10);
// Create path.
var path = new Path({
segments: [
[100, 100],
[200, 100],
[260, 170],
[360, 170],
[420, 250]
],
strokeColor: 'red',
strokeWidth: 10
});
// Translate given thing along global vector.
function translateThing(thing) {
switch (thing.getClassName()) {
case 'Path':
thing.position += vector;
break;
case 'Curve':
thing.segment1.point += vector;
thing.segment2.point += vector;
break;
case 'Segment':
thing.point += vector;
break;
}
}
// On mouse down...
function onMouseDown(event) {
// ...only select what was clicked.
path.selected = false;
hit = paper.project.hitTest(event.point);
if (hit && hit.location) {
hit.location.curve.selected = true;
}
else if (hit && hit.segment) {
hit.segment.selected = true;
}
// We check all items for demo purpose.
// Move all selected things.
// First get selected items in active layer...
project.activeLayer.getItems({ selected: true })
// ...then map them to what is really selected...
.map(getSelectedThing)
// ...then translate them.
.forEach(translateThing);
}
// This method returns what is really selected in a given item.
// Here we assume that only one thing can be selected at the same time.
// Returned thing can be either a Curve, a Segment or an Item.
function getSelectedThing(item) {
// Only check curves and segments if item is a path.
if (item.getClassName() === 'Path') {
// Check curves.
for (var i = 0, l = item.curves.length; i < l; i++) {
if (item.curves[i].selected) {
return item.curves[i];
}
}
// Check segments.
for (var i = 0, l = item.segments.length; i < l; i++) {
if (item.segments[i].selected) {
return item.segments[i];
}
}
}
// return item by default.
return item;
}
答案 0 :(得分:0)
const
定义了一个常量,因此您将无法更改y
和z
的值。相反,您应该使用var
或let
:
let y = 0;
let z = 0;
答案 1 :(得分:0)
考虑以“功能性”方式进行操作:
const checkXo = (str, a, b) =>
str.split('').filter(s => s === a).length ===
str.split('').filter(s => s === b).length
使用
进行测试checkXo('xxoo', 'x', 'o') // return true
checkXo('stackoverflow', 'x', 'o') // return false
请注意,单行代码可以检查相同数量的所选字符“ a,b”。