小编Tec*_*ner的帖子

数组角案例中的二进制搜索

我正在尝试实现二进制搜索,并且除特殊情况外,所有数字都可以正常工作:

const a = [1,2,3,4,5];

function findNum(arr, num) {
    let start=0, end = arr.length-1, mid = Math.floor((start+end)/2);

    while(start <= end) {
        mid = Math.floor((start+end)/2);
        if(mid===num) return true;
        else if(mid > num) end = mid-1;
        else start = mid+1;
    }
    return false;    
}

console.log(findNum(a, 5));
Run Code Online (Sandbox Code Playgroud)

当我搜索“ 5”时,它返回false,而不是true。我在这里想念我什么?

所有其他情况都可以正常工作。

javascript binary-search

5
推荐指数
1
解决办法
51
查看次数

MomentJS从00:00到23:59

我一直试图添加这个滑块:http: //ionden.com/a/plugins/ion.rangeSlider/demo_advanced.html

这是我的示例代码:

$("#range").ionRangeSlider({
    min: +moment().subtract(12, "hours").format("X"), //need to change to 00:00
    max: +moment().format("X"), //need to change this to 23:59
    from: +moment().subtract(6, "hours").format("X"), 
    grid: true,
    force_edges: true,
    prettify: function (num) {
        var m = moment(num, "X").locale("en");
        return m.format("HH:mm");
    }
});
Run Code Online (Sandbox Code Playgroud)

我想将min添加到00:00,最大值添加到23:59

我试过了

moment("1234", "hmm").format("HH:mm")
Run Code Online (Sandbox Code Playgroud)

我收到无效日期或NaN.

请帮我.我不确定我哪里出错了.

javascript momentjs

4
推荐指数
1
解决办法
2127
查看次数

了解javascript中的事件队列和调用堆栈

我对理解"事件队列"和"调用堆栈"概念的好奇心在我解决这个问题时开始:

var list = readHugeList();

var nextListItem = function() {
    var item = list.pop();

    if (item) {
        // process the list item...
        nextListItem();
    }
};
Run Code Online (Sandbox Code Playgroud)

如果数组列表太大,以下递归代码将导致堆栈溢出.你如何解决这个问题仍然保留递归模式?

提到的解决方案是这样的:

var list = readHugeList();

var nextListItem = function() {
    var item = list.pop();

    if (item) {
        // process the list item...
        setTimeout( nextListItem, 0);
    }
};
Run Code Online (Sandbox Code Playgroud)

解:

由于事件循环处理递归而不是调用堆栈,因此消除了堆栈溢出.当nextListItem运行时,如果item不为null,则将超时函数(nextListItem)推送到事件队列并退出函数,从而使调用堆栈保持清零.当事件队列运行其超时事件时,将处理下一个项目并设置计时器以再次调用nextListItem.因此,在没有直接递归调用的情况下从开始到结束处理该方法,因此无论迭代次数如何,调用栈都保持清晰.

现在我的问题:

Q1)"事件队列"和"调用堆栈"之间有什么区别

Q2)我不明白答案.有人可以详细解释我吗?

Q3)当我在javascript中执行函数或调用变量或对象时.流程怎么样? 什么在调用堆栈?(比方说我做setTimeout ..它是去callstack还是事件队列?)

这些概念非常不清楚.我用谷歌搜索,但大多数结果不是我期望理解的.

请帮忙!

javascript recursion

4
推荐指数
1
解决办法
1183
查看次数

带按钮和图像的工具提示

我正在尝试创建一个带按钮的工具提示(就像LinkedIn一样).

这是我到目前为止所尝试的:(但是从这里开始)

.tooltip{
  display: inline;
  color:black;
  position: relative;
  top:200px;
}

.tooltip:hover:after{
    background: grey;
    border-radius: 5px;
    color: #fff;
    content: attr(title);
    left: 20%;
    padding: 5px 15px;
    position: absolute;
    top: -53px;
    width: 163px;
}

.tooltip:hover:before{
  border:solid;
  border-color: #333 transparent;
  border-width: 6px 6px 0px 6px;
  bottom: 20px;
  content: "";
  left: 50%;
  position: absolute;
  z-index: 99;
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>repl.it</title>
        <script src="index.js"></script>
        <link href="index.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
      <a href="#" title="This is some information for our tooltip." …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

4
推荐指数
1
解决办法
6556
查看次数

如何转到量角器中的特定链接?

我对量角器很陌生。我想明确地告诉量角器打开一个特定的URL并执行一些操作。我怎样才能做到这一点?

这就是我目前正在做的事情。

((jasmine, driver) ->
    helpers = require(process.cwd() + '/../common/test/lib/helpers.coffee')

    timeout = helpers.defaultTimeout

    ##############  Test cases  ##################
    describe 'Going to the Connect pages and launch at dashboard', ->
        it 'Should login as admin and launch Location view', ->
            helpers.login(driver)
            expect(driver.wait ( ->
                return driver.getCurrentUrl().then (url) ->
                    return /map/.test(url) && /loc/.test(url)
            ), timeout).toBeTruthy()
        it 'should navigate to the connect page and show dashboard view', ->
            element(By.xpath("//a[@href='/connection/']")).click()
            expect(driver.wait ( ->
                return driver.getCurrentUrl().then (url) ->
                    return /dashboard/.test(url) && /conn/.test(url)
            ), timeout).toBeTruthy()

)(jasmine, browser.driver) …
Run Code Online (Sandbox Code Playgroud)

javascript jquery selenium protractor

3
推荐指数
1
解决办法
8663
查看次数

总和函数currying

我正在尝试编写一个执行以下操作的求和函数:

sum(1)(2)(3) => returns 6

但是,我的解决方案很难.我知道我犯了一个愚蠢的错误,有人能指出我正确的方向吗?

我的实施:

function add(args) {
  let sum = args[0];
  let func = function(...args2) {
    if (!args2[0]) return sum;
    sum += args2[0];
    return func;
  }
  return func;
}


add(1)(2)(3);
Run Code Online (Sandbox Code Playgroud)

另外,我可以编写一个执行以下操作的通用函数吗?add(1)(2)(3)或add(1)(2)(3)()=> 6

javascript currying

3
推荐指数
1
解决办法
176
查看次数

在JavaScript中实现节点存储功能

在ES5和ES6中建立商店时,有人问我这个问题。

我一直在尝试解决此问题,但是在如何存储节点上遇到了麻烦

实施商店类:

使用set(Node,value),get(Node)和has(Node)方法实现存储类,该方法存储给定Node的对应值。

这就是我能够写的(伪代码)

function Store () {
    this.store = [];
}

Store.prototype.set = function(node, v) {
    // Problem here would be how do I store the node?
}

Store.prototype.get = function(node) {
    if(this.has(node)) {
        return this.store.find(each => {
            // Check to see if it's the same node and return.
        })
    }
}

Store.prototype.has = function(node) {
    return this.store.indexOf(node) > -1;
}
Run Code Online (Sandbox Code Playgroud)

注意:我们可能正在商店中存储HTML DOM。因此,键将是“ DOM”元素而不是字符串。

有人可以给我一个例子吗?我想这会像ES6中的Map一样工作。如果要在ES5中实现,该如何首先存储DOM节点?

javascript dom

3
推荐指数
1
解决办法
88
查看次数

你如何找到阵列中最高的三个数字?

我有一个问题:

给定一个arrayOfInts,找到可以从三个整数中获得的最高产品.

输入:var a = [1,7,9,2]; 预期产出:7*9*2 = 126

我们如何解决这个问题?我写了代码但是徒劳无功.

function highestProductIntegers(a){
  var h1, h2,h3; //three highest number pointers.

  a.forEach(function(val, index){
    if(index=0){
      h1 = val; //assign the first element to h1 (Highest 1)
    }

    h2 = val; //second to h2

    if( val > h1 ){ //if second is greater than h1 make it h1 || h2 || h3
      h3 = h2;
      h2 = h1;
      h1 = val;
    }
    else if(val<h1 && val > h2) //keep comparing for all elements.
      h2 …
Run Code Online (Sandbox Code Playgroud)

javascript arrays

2
推荐指数
1
解决办法
146
查看次数

日语数字正则表达式

**问题:**我如何只允许日语中的数字(平假名/ Katagana)

使用案例:我想取"数字"字段(我的输入应该只接受英文和日文的数字)

我写了以下示例代码段:

// The goal of this demo is to demonstrate the RegEx patterns for English and Japanese Chracters

var english = "09c12";
var japanese =  "?????b";


console.log("-----English Test---")
console.log(english.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")


console.log("-----japanese Test---")
console.log(japanese.replace(/[^0-9\/]/gi, ''));
console.log("--------------------")
Run Code Online (Sandbox Code Playgroud)

问题: 这对日语不起作用.请赐教.

我假设日文字符有不同的ASCII/Unicode值?

请帮我修复代码.我想确保用户输入数字.

谢谢

javascript regex

2
推荐指数
1
解决办法
851
查看次数

使用 Promise 的 JavaScript 调度器实现

我正在解决这个有趣的 javascript 问题(面试问题),但我一直在思考如何使用 promise 来实现它。

问题:

用 JavaScript 编写一个调度程序,它接受最大并发任务数作为参数并调度任务(每个任务可能需要任意时间才能完成)。

请注意,在继续执行其他任务之前,我们一次只需要执行“n”个(并发)任务。

这是我的实现:

var exampleTaskA = function () {
    setTimeout(function () {
        console.log('Task A Done');
    }, 1000);
};

function TaskRunner(concurrency) {
    this.limit = concurrency;
    this.store = [];
    this.len = this.store.length;
}

TaskRunner.prototype.push = function (task) {
    this.store.push(task);
    function getWorker(store, limit) {
        if(!store.length) return;

        if(store.length <= limit) {
            const cur = store.shift();
            if(cur) cur();
            getWorker(store, limit);
        }
    }

    getWorker(this.store, this.limit);
}

var task = new TaskRunner(2);
console.log(task.push(exampleTaskA));
console.log(task.push(exampleTaskA));
console.log(task.push(exampleTaskA)); 
console.log(task.push(exampleTaskA));
console.log(task.push(exampleTaskA));
console.log(task.push(exampleTaskA));
console.log(task.push(exampleTaskA)); …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous

2
推荐指数
1
解决办法
1341
查看次数