nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

Node gives different results for two kind of => function which are ought to be the same #4256

Closed cjf9 closed 9 months ago

cjf9 commented 9 months ago

Version

v20.7.0

Platform

Microsoft Windows NT 10.0.19045.0

Subsystem

sorry I am an uneducated Chinese farmer

What steps will reproduce the bug?

function quicksort(lst, fun){
    const len = lst.length;
    if(len <= 1){
        return lst;
    }
    else{
        let pivot = lst[0]
        let left  = []
        let right = []
        for(let i = 1; i < len; i++){
            if(fun(lst[i], pivot)){
                left.push(lst[i]);
            }
            else{
                right.push(lst[i]);
            }
        }
        //console.log(left.concat([pivot]).concat(right));
        //console.log("\n---\n");
        return quicksort(left, fun).concat([pivot]).concat(quicksort(right, fun));
    }
}

let arr = [5,3,2,3,1,3,2,3];
let le = (a,b) => {a <= b};           // console.log(quicksort(arr, le)) gets the original array [5,3,2,3,1,3,2,3]
//let le = (a,b) => {return a <= b};  // gets the sorted array
//let le = (a,b) => a <= b;           // gets the sorted array
console.log(quicksort(arr, le));

How often does it reproduce? Is there a required condition?

simply every time

What is the expected behavior? Why is that the expected behavior?

I think all of they three functions should give the same result. I am not so sure because I am just start learning.

What do you see instead?

like above ↑

Additional information

nope. thank you guys for your work.

benjamingr commented 9 months ago

I think all of they three functions should give the same result.

What three functions are you referring to?

richardlau commented 9 months ago

@cjf9 Your expectation is incorrect. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body

cjf9 commented 9 months ago

thank you sir. sorry to bother you

------------------ 原始邮件 ------------------ 发件人: "Richard @.>; 发送时间: 2023年10月3日(星期二) 晚上8:00 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [nodejs/help] Node gives different results for two kind of => function which are ought to be the same (Issue #4256)

@cjf9 Your expectation is incorrect. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#function_body

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>