Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random array #92

Open
xgqfrms opened this issue Nov 14, 2019 · 5 comments
Open

random array #92

xgqfrms opened this issue Nov 14, 2019 · 5 comments

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Nov 14, 2019

image

"use strict";

/**
 * 
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-11-14
 * 
 * @description 
 * @augments 
 * @example 
 * @link https://github.com/haizlin/fe-interview#%E8%B5%B7%E6%BA%90
 * 
 */

const log = console.log;

const arr = [];
const recursiveFun = (arr = [], len = 5,  min = 2, max = 32) => {
    const rand = Math.floor(Math.random() * (max - min) + min);
    if(arr.length < len){
        if(!arr.includes(rand)){
            arr.push(rand);
        }
        recursiveFun(arr);
    }
}
recursiveFun(arr);

log(`random array`, arr);

https://github.com/haizlin/fe-interview#%E8%B5%B7%E6%BA%90

@xgqfrms
Copy link
Owner Author

xgqfrms commented Nov 14, 2019

@xgqfrms
Copy link
Owner Author

xgqfrms commented Nov 14, 2019

https://github.com/yygmind/blog/issues/5

https://github.com/haizlin/fe-interview/issues

https://github.com/qiu-deqing/FE-interview

https://github.com/Advanced-Frontend/Daily-Interview-Question

@xgqfrms
Copy link
Owner Author

xgqfrms commented Nov 14, 2019

"use strict";

/**
 * 
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-11-14
 * 
 * @description js 生成 2-32 之间的随机数
 * @augments 
 * @example 
 * @link https://github.com/haizlin/fe-interview#%E8%B5%B7%E6%BA%90
 * 
 */

let log = console.log;


const recursiveFun = (arr = [], len = 5, min = 2, max = 32) => {
    const rand = Math.floor(Math.random() * (max - min) + min);
    if(arr.length < len && !arr.includes(rand)){
        arr.push(rand);
        return recursiveFun(arr);
    } else {
        return arr;
    }
}

const arr = recursiveFun();
log(`random array`, arr);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values

@xgqfrms
Copy link
Owner Author

xgqfrms commented Nov 14, 2019

{
    "JavaScript ES6 React Template": {
    "prefix": "js6r",
    "body": [
        "\"use strict\";",
        "",
        "/**",
        " * ",
        " * @author xgqfrms",
        " * @license MIT",
        " * @copyright xgqfrms",
        " * @created 2019-11-1$",
        " * ",
        " * @description $2",
        " * @augments $3",
        " * @example $4",
        " * @link $5",
        " * ",
        " */",
        "",
        "let log = console.log;",
        "",
        "const $2Generator = ($5datas = [], debug = false) => {",
        "    let result = ``;",
        "    // do something...",
        "    return $7result;",
        "};",
        "",
        "",
        "",
        "export default $2;",
        "",
        "export {",
        "    $2,",
        "};",
        "",
    ],
    "description": "JavaScript ES6 React Template & code snippets!"
    }
}

@xgqfrms
Copy link
Owner Author

xgqfrms commented Nov 14, 2019

js env check

"use strict";

/**
 * 
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-11-14
 * 
 * @description 写个方法判断当前脚本运行在浏览器还是node环境中 js
 * @augments 
 * @example 
 * @link https://github.com/haizlin/fe-interview/issues/1514#issuecomment-553188594
 * 
 */

let log = console.log;

log(`js env`, typeof window === 'undefined' ? 'Node' : 'Browser');


log(`Node global`, global);
log(`Browser window`, window);
// ReferenceError: window is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant