!!this.list[this.front] 为什么是双叹号?

来源:8-2 循环队列-代码实操

nkliyc

2019-06-25

class MyCircularQueue{
    constructor (k) {
        // 用来保存数据长度为K的数据结构
        this.list = Array(k)
        // 队首指针
        this.front = 0
        // 队尾指针
        this.rear = 0
        // 队列长度
        this.max = k
    }
    enQueue (value) {
        if(this.isFull()) {
            return false
        } else {
            this.list[this.rear] = value
            this.rear = (this.rear + 1) % this.max
            return true
        }
    }
    deQueue () {
        if(this.isEmpty()) {
            return false
        } else {
            this.list[this.front] = null
            this.front = (this.front + 1) % this.max
            return true
        }

    }
    Front () {
        if(this.isEmpty()) {
            return -1
        } else {
            return this.list[this.front]
        }
    }
    Rear () {
       if(this.isEmpty()) {
            return -1
        } else {
            let rear = (this.max + this.rear - 1) % this.max
            return this.list[rear]
        }
    }
    isEmpty () {
        return this.front === this.rear && !this.list[this.front]
    }
    isFull () {
        return this.front === this.rear && !!this.list[this.front]
    }

};
 isFull () {
        return this.front === this.rear && !!this.list[this.front]
    }

​老师,!!this.list[this.front] 这个为什么必须要两个叹号,是这样的时候:

isFull () {
        return this.front === this.rear && this.list[this.front]
    }

leetcode通不过

写回答

2回答

快乐动起来呀

2019-06-25

双叹号是将变量转化为布尔值,同学我建议你不要刻意去盯着是不是满足了所有的leetcode测试用例,咱们的课程侧重讲算法思想和方法,对一些极端的边界没有刻意打补丁,希望能抓住重点内容

0
1
nkliyc
非常感谢!我已找到问题所在。
2019-06-25
共1条回复

nkliyc

提问者

2019-06-25

    

前言

    首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 false, 其他(包括{}, [], Infinity)为true.

    可以使用Boolean()函数或是两次取非就能获得对象的布尔值,例如Boolean(undefined)和!!undefined同样能取得布尔值false,

    对于0, '', null, undefined, NaN,{}, [], Infinity求布尔值,分别是false false false false false true true true.

因此我们知道的一点是:对象的布尔值是true,即使是对象{}。


bool值转换


    数据类型        bool值转化    
    
    undefined    undefined 转化为 false    
    Object       null 转化为false,其他为 true    
    Boolean      false 转化为 false,true 转化为 true    
    Number       0,NaN 转化为false,其他为 true    
    String       "" 转化为 false,其他为 true


"&&"

javascript中“&&”运算符运算法则如下:

如果&&左侧表达式的值为真值,则返回右侧表达式的值;否则返回左侧表达式的值。多个&&表达式一起运算时,返回第一个表达式运算为false的值,如果所有表达式运算结果都为true,则返回最右侧一个表达式运算的值。

    const aa = {'name': 'xx'};    
    const bb = aa && aa.age; // bb输出为undefined;
    let cc;
    const dd = cc && cc.name ? cc.name : undefined; // dd输出为undefined
    const dd = cc && cc.name; // dd输出为undefined;

   

"||"

javascript中"||"运算符的运算法则如下:

如果"||"左侧表达式的值为真值,则返回左侧表达式的值;否则返回右侧表达式的值。多个"||"表达式一起运算时,返回第一个表达式运算结果为true的值,如果所有表达式运算结果都为false,否则返回最右侧的表达式的值。

    const aa = false || 'xx'; // aa输出为'xx'

   


"!!"

"!!"将表达式进行强制转化为bool值的运算,运算结果为true或者false。

    const aa = 'xx';
    const bb = !!aa; // bb输出为true
    const cc = !!(NaN || undefined || null || 0 || '' ); // cc为false;

  


3
2
快乐动起来呀
回复
nkliyc
纸上得来终觉浅,绝知此事要躬行,很赞
2019-06-25
共2条回复

JavaScript版 数据结构与算法

填补前端同学的算法短板,掌握面试中最常见的算法与数据结构

2467 学习 · 395 问题

查看课程