mercredi 30 mars 2016

Javascript doesn't return the value / used creatElement() and element.id

I'm new in Javascript, I'm doing a project that its main idea is having a conversation with 'Alex', however, it was working well till I wanted to input and output in -messaging way-, everthing went wrong after it. I used createElement() , appendChild() and element.id="id" in the write() and getVal() functions I do think that the error are from there. Console: There's no errors have been given by the console , but when I type anything in the textbox it type in the div but the function write doesn't return the value. Sorry for my English ^_^

I don't know if the issue from css or not.

Body:

userName = " Medardo";
var date = new Date().toTimeString().split(" ")[0];
var d = new Date();
var weekdays = new Array(7);
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
var today = weekdays[d.getDay()];

//
var firstHello = [
  [greatings() + userName + ", How can I help you ?"],
  ["Hi" + userName + " how can I help ?"],
  [greatings() + ", how can I  help?"]
];

//
var noWords = ["?", "!", "?!", "!?", "alex", "alex!", "alex?"];

//
function randomArray(array) {
    var index = Math.floor(Math.random() * array.length);

    return array[index];
  }
  //find index !

function findIndex(array, something) {

  for (var i = 0; i < array.length; i++) {

    if (array[i] === something) {
      return i;
    }

  }


};
/////////////////////////////////////////////////////////
function deleteVals(array, array2) {
    var return_val;
    console.log("starting the function , array,array2 = " + array + "    " + array2);
    if (typeof array == "string") {
      array = array.split(" ");
    }

    if (array[length - 1] === " ") {
      array.splice(length - 1, 1);
    }

    console.log("spilted array, array= " + array);
    for (var i = 0; i < array.length; i++) {
      console.log("We made the for loop with i as variable , i = " + i);

      for (var x = 0; x < array2.length; x++) {
        console.log("We made the second for loop with x as variable" + x);
        console.log("We made the if condation the condtoin is 'array[i] ===     array[x] ,in another way = array[" + i + "] === array[" + x + "] ... in another   way     = " + array[i] + "===" + array2[x])

        if (array[i] === array2[x]) {
          array.splice(i, 1);
          console.log("we deleted array[i] in another way = array[" + i + "]     the result of delere array[i]=" + array[i]);
          console.log("definding return_val as : return_val = array a-way = return_val =" + array)
          return_val = deleteVals(array, noWords);

        } else {
          console.log("starting else atatment ")
          console.log("definding return_val as : return_val = array a-way = return_val =" + array)

          return_val = array;


        }



      }
      console.log("finishing the second for loop");

    }

    console.log("finishing the first for loop");
    console.log("finishing the function deleteVal() ... and it's return :     return_val.join(' ') a-way =" + return_val.join(" "));

    return return_val.join(" ");
  }
  //////////////////////////////////////////////////////////

function getVal() {
    var xxx = document.getElementById("textbox").value;
    var para = document.createElement("p");
    var newLine = document.createTextNode(xxx);
    para.appendChild(newLine);
    para.id = "userText";
    document.getElementById("contText").appendChild(para);
    console.log("newline=" + newLine);
    log("para= " + para);
    xxx = para.innerHTML;
    log("xxx=" + xxx)
    return deleteVals(minimize(xxx), noWords);
  }
  //var ask = prompt ("Ask me anything >>").toLowerCase ();
  //*********Check here

function write(x) {
  var para = document.createElement("P");
  var newLine = document.createTextNode(x);
  para.appendChild(newLine);
  para.id = "compText";
  document.getElementById("contText").appendChild(para);
};

function log(x) {
  console.log(x)
}


//Capitalize the first letter func :
function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

function minimize(string) {
  return string.toLowerCase();
};

//..............You can ignore all of this

function greatings() {
  var greating;
  if (date >= "06:00:00" && date <= "11:00:00") {
    var x = [
      ["Good morning"],
      ["It' such a great morning ! "],
      ["It's look like a great morning !"],
      ["Morning ! "],
      ["<strong>I remind myself every morning: Nothing I say this day will teach me anything. So if I'm going to learn, I must do it by listening.</strong> "],
      ["I get up in the morning looking for an adventure. "],
      ["If you're changing the world, you're working on important things. You're excited to get up in the morning. "],
      ["SET A GOAL THAT MAKES YOU WANT TO JUMP OUT OF THE BED IN THE MORNING. "],
      ["Sir ,,, you open two gifts this morning ther were your eyes. "],
      ["YOU WILL NEVER HAVE THIS DAY AGAIN , SO MAKE IT COUNT. "],
      ["Each good morning we are born again, what we do today is what matters most "],
      ["TRY TO NOT STOP THIS MORNING "],
      ["Learn somthing new everyday ! that's the rule. "]
    ]

    greating = "Good morning ";
  } else if (date > "11:00:00" && date <= "15:00:00") {
    var x = [
      ["Good afternoon "],
      ["It' such a great afternoon ! "],
      ["It's look like a great afternoon ! "],
      ["Hope you having great day "],
      ["Hope you having great day "],
      ["Hope you having great day "]
    ]

    greating = randomArray(x);

  } else if (date > "15:00:00" && date <= "22:00:00") {
    var x = [
      ["Good evening "],
      ["How such a great evening ! "],
      ["It's look like a great evening ! "],
      ["What a nice night for an evening. "]
    ]
    greating = randomArray(x);
  } else {
    var x = [
      ["You should have some rest !"],
      ["Close your eyes a lil bit !"],
      ["I think you must  sleep now !"],
      ["It's Late for a human !"]
    ];

    greating = randomArray(x);
  };
  return greating;
};

function randomAnswer(array) {


  for (var i = 0; i < array[0].length; i++) {
    if (getVal() === array[0][i]) {
      write((randomArray(array[1])));
    }
  }
};
write(randomArray(firstHello));
//...............................
function dosome() {
  //arrays ^_^

  var hellos = [
    ["hi", "hello", "what's up", "hey", "heyyy", "heyyyy", "hola", "marhaba",
      "hla", "shini aljo", "shini algo"
    ],
    ["Hi" + userName, "Hi there", greatings() + userName]
  ];

  var hows = [
    ["how are you", "what's up", "hows you", "how is you", "how you  doing"],
    ["I'm fine , thanks", "I'm great" + userName + ", thanks",
      "I'm pretty good , thanks you"
    ]
  ]

  var who_are_u = [
    ["who are you", "what are you", "you are ?", "what is you", "what is your name", "what can I call you"],
    ["I'm Alex !", "I'm Alex.", "I am Alex", "My name is Alex"]
  ];

  var what_time = [
    ["what time is it", "what's the time", "guess what time is it", "tell me the time", "the time ?"],
    ["It's :" + date, "I'm pretty sure that the time is :" + date, "The time is :" + date, "I'm sure that it's :" + date]
  ];

  var when_lastModified = [
    ["what time is that i last modified", "when i last modified", "when the last time i visted you", "when last time i opened you"],
    ["That was: " + document.lastModified, "It was: " + document.lastModified, "According to my info , it was: " + document.lastModified,
      "I'm pretty sure it was: " + document.lastModified
    ]
  ];

  var thanks = [
    ["thanks", "thank you", "thank you very much", "thanks a lot", "thank you a lot", "thanks very much"],
    ["No need to thank me" + userName, "That's my work" + userName, "Anytime" + userName, "Welcome !"]
  ];


  //calls ----------->

  randomAnswer(hellos);
  randomAnswer(hows);
  randomAnswer(who_are_u);
  randomAnswer(what_time);
  randomAnswer(when_lastModified);
  randomAnswer(thanks);

  ////

  //
  var spilted_q = getVal().split(" ");
  switch (spilted_q[0]) {
    case "my":
      switch (spilted_q[1]) {
        case "name":
          switch (spilted_q[2]) {
            case "is":
              userName = " " + capitalize(spilted_q[3]) + " ";

              alert("Your name has been saved" + userName)
              write("Hi" + userName)
              break;
          }
          break;
      }
      break;

  }
  return userName
};

function deleteVal() {

  var x = document.getElementById("compText").innerHTML = "";
  var y = document.getElementById("userText").innerHTML = "";
  return x, y;
}
body {
  background-color: #808080;
  font-family: 'Times New Roman', Times, serif;
  color: red;
}
#contText {
  text-align: center;
  border: 3px black solid;
  position: relative;
  left: auto;
  right: auto;
  top: 30px;
  width: 400px;
  height: 300px;
  background-color: white;
}
#allthings {
  text-align: center;
  position: relative;
  top: 50px
}
#compText {
  text-align: left;
}
#userText {
  text-align: right;
}
<div id="contText"></div>
<div id="allthings">
  <input id="textbox" type="text" placeholder="Write your question here..."></input>
  <input id="send" type="button" value="Send" onclick="getVal()"></input>
  <input id="delete" type="button" value="Delete" onclick="deleteVal()"></input>
</div>

Aucun commentaire:

Enregistrer un commentaire