我只被允许使用google文档来写作。
你能告诉我我做错了什么吗?当我问招聘人员为什么我失败时,她不会回复我。任务1:实现函数verify(text),验证文本中的括号是否 正确嵌套。您需要考虑三种类型:(),[],<>
我的回答:
const verify = (text) => {
const parenthesesStack = [];
for( let i = 0; i<text.length; i++ ) {
const closingParentheses = parenthesesStack[parenthesesStack.length - 1]
if(text[i] === “(” || text[i] === “[” || text[i] === “<” ) {
parenthesisStack.push(text[i]);
} else if ((closingParentheses === “(” && text[i] === “)”) || (closingParentheses === “[” && text[i] === “]”) || (closingParentheses === “<” && text[i] === “>”) ) {
parenthesisStack.pop();
}
};
return parenthesesStack.length ? 0 : 1;
}
任务2:尽可能地简化下面的实现。如果您还可以将性能作为简化的一部分来提高,那就更好了!仅供参考:此代码超过35行,超过300个令牌,但它可以写入5行,不到60个记号。功能
//' a '和' b '是单个字符串
function func2(s, a, b) {
var match_empty=/^$/ ;
if (s.match(match_empty)) {
return -1;
}
var i=s.length-1;
var aIndex=-1;
var bIndex=-1;
while ((aIndex==-1) && (bIndex==-1) && (i>=0)) {
if (s.substring(i, i+1) == a)
aIndex=i;
if (s.substring(i, i+1) == b)
bIndex=i;
i--;
}
if (aIndex != -1) {
if (bIndex == -1)
return aIndex;
return Math.max(aIndex, bIndex);
} else {
if (bIndex != -1)
return bIndex;
return -1;
}
};
我的回答:
const funcSimplified = (s,a,b) => {
if(s.match(/^$/)) {
return -1;
} else {
return Math.max(s.indexOf(a),s.indexOf(b))
}
}
首先,我会明确招聘人员的具体要求。
其次,从你的第一个"for"语句开始,我就会让你失败。见我的注释:
// Bonus - add jsdoc description, example, expected variables for added intention.
const verify = (text) => {
// verify what? be specific.
const parenthesesStack = [];
for( let i = 0; i<text.length; i++ ) {
// this could have been a map method or reduce method depending on what you were getting out of it. Rarely is a for loop like this used now unless you need to break out of it for performance reasons.
const closingParentheses = parenthesesStack[parenthesesStack.length - 1]
// parenthesesStack.length - 1 === -1.
// parenthesesStack[-1] = undefined
if(text[i] === “(” || text[i] === “[” || text[i] === “<” ) {
parenthesisStack.push(text[i]);
// “ will break. Use "
// would have been more performant and maintainable to create a variable like this:
// const textOutput = text[i]
// if (textOutput === "(" || textOutput === "[" || textOutput === "<") {
parenthesisStack.push(textOutput)
} else if ((closingParentheses === “(” && text[i] === “)”) || (closingParentheses === “[” && text[i] === “]”) || (closingParentheses === “<” && text[i] === “>”) ) {
parenthesisStack.pop();
// There is nothing in parenthesisStack to pop
}
};
return parenthesesStack.length ? 0 : 1;
// Will always be 0.
}
不完全是你的功能或逻辑的意图在做什么,但它会失败,根据我所看到的。在浏览器中测试它,或者使用typescript playground。你也可以在里面写javascript。
没有招聘人员的反馈很难判断。但是我敢说你误解了第二个功能。
func2("mystrs", 's', 'm') // returns 5
funcSimplified("mystrs", 's', 'm') // returns 3
您返回的是Math.max(s.indexOf(a),s.indexOf(b))
而不是Math.max(s.lastIndexOf(a), s.lastIndexOf(b))
原始代码从i=len(str) - 1
开始,减少到0
。他们正在反向读取字符串
一个可能的实现是
const lastOccurenceOf = (s,a,b) => {
// Check for falsyness (undefined, null, or empty string)
if (!s) return -1;
// ensure -1 value if search term is empty
const lastIndexOfA = a ? s.lastIndexOf(a) : -1
const lastIndexOfB = b ? s.lastIndexOf(b) : -1
return Math.max(lastIndexOfA, lastIndexOfB)
}
或者一个更简洁的例子,它可能更糟糕(因为可读性更差)
const lastOccurenceOf = (s,a,b) => {
const safeStr = s || '';
return Math.max(safeStr.lastIndexOf(a || undefined), safeStr.lastIndexOf(b || undefined))
}
我使用a || undefined
强制a为未定义,如果它是一个空字符串,因为:
"canal".lastIndexOf("") = 5
"canal".lastIndexOf(undefined) = -1
如果a或b为空,原函数将返回-1
另外,您是否问过是否允许使用ES6+语法?你已经得到了一个普通的JS和你实现等效使用ES6+。有些招聘者有恶性POV。