I am trying to add and retrieve cookie using JavaScript. My code is
   function setCookie(name,value,days) {
                    var date = new Date();
                    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                    var expires = "; expires=" + date.toGMTString();            
                    document.cookie = name + "=" + value + expires + ";";
                    alert("Hello you are : "+getCookie(name)); // displaying alert message 'Hello you are :'
            }
            function getCookie(cname) { //any problem with this code?
                var rcookies = document.cookie;
                var cookiearray = rcookies.split(";");
                for (i=0;i<cookiearray.length;i++){
                    var value=cookiearray[i];
                    value=value.split("=");
                if(value[0]==cname){
                    return value[1];
                }
                }
                return "";
            }
    
    function checkCookie() {
        var user = getCookie("user");
        if (user != "") {
            alert("You are " + user); // This alert is Never displayed But it need to be. 
        } else {
            user = document.getElementById("name").value;
            alert(user); // displaying the value of text field
            setCookie("user", user, 1);
        }
    }
    <form>
     <input type="text" id="name" name="name" required="required">
     <input type="button" onclick="checkCookie();" id="buttons" name="button" value="cookie">
    </form>
   
The problem is when I click on the button it set the cookie(I saw it in 'view site information', it was there) But when I try to get the cookie using getCookie method it's not showing the cookie value saved in cookies. And also when I refresh the page and press the 'view site information' icon to see cookie again, it shows 0 cookies there.
what's wrong with the code? Or any mistake I'm doing?