我正在用 ruby 构建一个简单的面包屑,但我不确定如何真正实现我的逻辑。
假设我有一组取自我的单词,request.path.split("/) ["", "products", "women", "dresses"]
我想将字符串推入另一个数组,所以最后我有["/", "/products", "products/women", "products/women/dresses"],我将使用它作为我的面包屑解决方案。
我不擅长红宝石,但现在我想出了以下方法
cur_path = request.path.split('/')
cur_path.each do |link|
arr = []
final_link = '/'+ link
if cur_path.find_index(link) > 1
# add all the previous array items with the exception of the index 0
else
arr.push(final_link)
end
end
Run Code Online (Sandbox Code Playgroud)
结果应该是 ["/", "/products", "/products/women", "/products/women/dresses"]
我的功能似乎不起作用,我不知道为什么。它应该返回 true 或 false 但它没有。我正在尝试计算该值是否小于 0,如果是,则返回 false,否则返回 true。
function tickets(peopleInLine){
// ...
var twentyfive = 0;
var fifty = 0;
var hundred = 0;
function checkforsales() {
if ((twentyfive < 0) || (fifty < 0) || (hundred < 0)) {
return false
} else {
return true
}
}
for (let i = 0; i < peopleInLine.length; i++) {
if(peopleInLine[i] === 25) {
twentyfive = twentyfive + 25;
} else if (peopleInLine[i] === 50) {
fifty = fifty + 50; …Run Code Online (Sandbox Code Playgroud)