, JSBIN LINK ( ).
, , , , .
Array.find(), , , , .
HTML:
<div id="container">
<textArea id="inputbox"></textArea></div>
<p id="output">output will show here</p>
JS:
var input,
container,
output;
function initDocElements() {
container = document.getElementById("container");
input = document.getElementById("inputbox");
output = document.getElementById("output");
}
function createArrayMap() {
var index,
tempChar,
tempStr = input.value,
len = tempStr.length,
arrMapKey = [],
arrMapValue = [];
for (var i = 0 ; i <len ; i++) {
tempChar = tempStr.charAt(i);
index = arrMapKey.indexOf(tempChar);
if ( index > -1) {
arrMapValue[index]++;
}
else {
arrMapKey.push(tempChar);
arrMapValue.push(1);
}
}
output.innerHTML = "array keys: "+arrMapKey.toString() +
"<br/>array values:"+arrMapValue.toString();
}
function keyPressHandler() {
createArrayMap();
}
function prepareEventHandlers() {
input.onfocus = function() {
if (this.value == "Start typing here!") {
this.value = "";
}
};
input.onblur = function() {
if (this.value === "") {
this.value = "Start typing here!";
}
};
input.onkeyup = keyPressHandler;
}
window.onload = function() {
initDocElements();
prepareEventHandlers();
};
BTW, , , , , char :
function createObject() {
var index,
tempChar,
tempStr = input.value,
len = tempStr.length,
freqObj = {};
for (var i = 0 ; i <len ; i++) {
tempChar = tempStr.charAt(i);
if (freqObj.hasOwnProperty(tempChar))
freqObj[tempChar]++;
else
freqObj[tempChar] = 1;
}
}
source
share