Emulate Array Objects - javascript

Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation doesn't exist either. Create a constructor called MyArray() that behaves as close to Array() as possible.
I thought it would be a good challenge to test my skills. This is what I came up with, but it doesn't work and is very incomplete.. I am a bit stumped:
function MyArray(){
// PRIVATE FIELDS -----------------------
var initialData = arguments;
var storage;
// PRIVATE METHODS ----------------------
function refresh(){ //this doesn't work :(
for(var i = 0; i < storage.length; i++){
this[i] = storage[i]
}
};
function initialize(){
storage = initialData;
refresh();
}
function count(){
var result = 0;
for(var item in this){
//console.log(item, parseInt(item), typeof item);
if(typeof item == 'number'){
result++;
}
}
return result;
};
initialize();
// PUBLIC FIELDS -------------------------
this.length = count();
// PUBLIC METHODS ------------------------
//todo:
this.push = function(item){
refresh();
}
this.pop = function(){}
this.join = function(){}
this.toString = function(){}
}
var c = new MyArray(32,132,11);
console.log(c, c.length);
This isn't for any production code or any project.. just to try to learn JavaScript a lot more. Can anyone try to help me with this code?

The thing is that you can use arguments object. It's not an array that was created with Array() so you won't break rules of the exercise. Here's what you need to do:
this.length = 0;
for (var i in arguments) {
this[this.length] = arguments[i];
this.length++;
}
I forgot to mention that ANY object is an associative array, so it's not a wrong thing to apply associative arrays for the exercise as we don't use the Array() object itself.
To author of the question: in your example you use: this["i"] = storage[i] that equals to this.i = storage[i]. Try to remove quotes and use it like this[i] = storage[i]

for(var item in this){
if(typeof item == 'number')
A property name is always a string. You'll need to check if it is the string representation of a number from 0 to MaxArrayLength. You could for example do
for (var i=0; i<4294967296; i++)
if (i in this)
result = i;
You might also be interested in these articles or the official specification for Array behaviour.

I'm currently looking through this book at the moment, I probably should try something more recent but its not that out of date yet and the principals are still sound...well they are in my opinion.
Anyway I went for a slightly different solution although I did take inspiration from the op in how he is handling strings. I think the challenge of this exercise is not to create any more arrays otherwise...its a bit of a crazy challenge, especially for people new to the language.
var MyArray = function () {
var args = arguments;
var length = 0;
for each(var item in args) {
this[length++] = item;
}
this.toString = function () {
var result = args[0];
for (var i = 1; i < args.length; i++) {
result += ',' + args[i];
}
return result;
}
this.length = length;
this.push = function (push) {
var newLength = args.length++;
args[newLength] = push;
this[newLength] = push;
return ++length;
}
this.pop = function () {
delete args[--args.length];
delete this[args.length];
length--;
return args;
}
this.join = function(joiner){
if(typeof arguments[0] === "undefined"){
joiner = ',';
}
var result = args[0];
for (var i = 1; i < args.length; i++) {
result += joiner + args[i];
}
return result;
}
}
var a = new MyArray(1, 2, 3, 'test');
console.log(a.toString());
console.log(a[a.length - 1]);
console.log(a.push('boo'));
console.log(a.toString());
console.log(a.pop());
console.log(a.toString());
console.log(a.join(','));
a.join(' isn\'t ');

My solution is:
function MyArray() {
this.length = 0;
for(i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
this.length++;
}
this.toString = function(joiner = ',') {
let str = this[0] ? this[0] : '';
for(i=1;i<this.length; i++) {
str+= joiner + this[i];
}
return str;
};
this.push = function(value) {
this[this.length++] = value;
return this.length;
};
this.pop = function() {
let value = this[this.length -1];
delete this[--this.length]
return value;
};
this.join = function(joiner) {
return this.toString(joiner);
}
}

Related

TypeError: function is not a function

Doing some jasmine testing with JS. So I have the testing function which seems reasonable(only running the first test currently)
describe('Anagram', function() {
it('no matches',function() {
var subject = new Anagram('diaper');
var matches = subject.matches([ 'hello', 'world', 'zombies', 'pants']);
expect(matches).toEqual([]);
});
Then I have my simple function
var Anagram = function(string){
this.word = string;
};
Anagram.prototype.matches = function(array){
var answer = [];
var splitWord = this.word.split('').sort();
for(var i = 0; i < array.length; i++){
var isAnagram = true;
var splitItem = array[i].split('').sort();
for(var j = 0; j < splitWord.length; j++){
if(splitWord[j] !== splitItem[j]){
isAnagram = false;
}
}
if(isAnagram === true){
answer.push(array[i]);
}
}
return answer;
};
module.export = Anagram;
My function is supposed to take a string and then look at an array of strings and return the anagram strings. I keep getting TypeError: Anagram is not a function. Tried looking for answers and most of it seemed to relate to semi colons which I think are not an issue since I use one after variable declaration and method declaration. I'd really just like to know what that typeError means if it's a common one to know or what the most likely reasons to get it are.
To check whether two strings are anagram or not, one can use this function. If the strings are anagram, then it will return true.
function isAnagram(str1,str2){
// str to alphabet array
let arr1 = Array.from(str1);
let arr2 = Array.from(str2);
//checking length
let n1 = arr1.length;
let n2 = arr2.length;
if (n1 !== n2){
return false;
}
//sorting array
arr1.sort();
arr2.sort();
for (let i=0; i<n1; i++ ){
if ( arr1[i] !== arr2[i]){
return false;
}
}
return true;
}
// Example: checking "listen and silent
console.log(isAnagram("listen", "silent"));

Check for repeated characters in a string Javascript

I was wondering if there is a way to check for repeated characters in a string without using double loop. Can this be done with recursion?
An example of the code using double loop (return true or false based on if there are repeated characters in a string):
var charRepeats = function(str) {
for(var i = 0; i <= str.length; i++) {
for(var j = i+1; j <= str.length; j++) {
if(str[j] == str[i]) {
return false;
}
}
}
return true;
}
Many thanks in advance!
This will do:
function hasRepeats (str) {
return /(.).*\1/.test(str);
}
(A recursive solution can be found at the end of this answer)
You could simply use the builtin javascript Array functions some MDN some reference
var text = "test".split("");
text.some(function(v,i,a){
return a.lastIndexOf(v)!=i;
});
callback parameters:
v ... current value of the iteration
i ... current index of the iteration
a ... array being iterated
.split("") create an array from a string
.some(function(v,i,a){ ... }) goes through an array until the function returns true, and ends than right away. (it doesn't loop through the whole array, which is good for performance)
Details to the some function here in the documentation
Here some tests, with several different strings:
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var text = texts[idx].split("");
document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");
}
//tested on win7 in chrome 46+
If you will want recursion.
Update for recursion:
//recursive function
function checkString(text,index){
if((text.length - index)==0 ){ //stop condition
return false;
}else{
return checkString(text,index + 1)
|| text.substr(0, index).indexOf(text[index])!=-1;
}
}
// example Data to test
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var txt = texts[idx];
document.write( txt + " ->" + checkString(txt,0) + "<br/>");
}
//tested on win7 in chrome 46+
you can use .indexOf() and .lastIndexOf() to determine if an index is repeated. Meaning, if the first occurrence of the character is also the last occurrence, then you know it doesn't repeat. If not true, then it does repeat.
var example = 'hello';
var charRepeats = function(str) {
for (var i=0; i<str.length; i++) {
if ( str.indexOf(str[i]) !== str.lastIndexOf(str[i]) ) {
return false; // repeats
}
}
return true;
}
console.log( charRepeats(example) ); // 'false', because when it hits 'l', the indexOf and lastIndexOf are not the same.
function chkRepeat(word) {
var wordLower = word.toLowerCase();
var wordSet = new Set(wordLower);
var lenWord = wordLower.length;
var lenWordSet =wordSet.size;
if (lenWord === lenWordSet) {
return "false"
} else {
return'true'
}
}
Using regex to solve=>
function isIsogram(str){
return !/(\w).*\1/i.test(str);
}
console.log(isIsogram("isogram"), true );
console.log(isIsogram("aba"), false, "same chars may not be adjacent" );
console.log(isIsogram("moOse"), false, "same chars may not be same case" );
console.log(isIsogram("isIsogram"), false );
console.log(isIsogram(""), true, "an empty string is a valid isogram" );
The algorithm presented has a complexity of (1 + n - (1)) + (1 + n - (2)) + (1 + n - (3)) + ... + (1 + n - (n-1)) = (n-1)*(1 + n) - (n)(n-1)/2 = (n^2 + n - 2)/2 which is O(n2).
So it would be better to use an object to map and remember the characters to check for uniqueness or duplicates. Assuming a maximum data size for each character, this process will be an O(n) algorithm.
function charUnique(s) {
var r = {}, i, x;
for (i=0; i<s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
On a tiny test case, the function indeed runs a few times faster.
Note that JavaScript strings are defined as sequences of 16-bit unsigned integer values. http://bclary.com/2004/11/07/#a-4.3.16
Hence, we can still implement the same basic algorithm but using a much quicker array lookup rather than an object lookup. The result is approximately 100 times faster now.
var charRepeats = function(str) {
for (var i = 0; i <= str.length; i++) {
for (var j = i + 1; j <= str.length; j++) {
if (str[j] == str[i]) {
return false;
}
}
}
return true;
}
function charUnique(s) {
var r = {},
i, x;
for (i = 0; i < s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charUnique2(s) {
var r = {},
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charCodeUnique(s) {
var r = [],
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s.charCodeAt(i);
if (r[x])
return false;
r[x] = true;
}
return true;
}
function regExpWay(s) {
return /(.).*\1/.test(s);
}
function timer(f) {
var i;
var t0;
var string = [];
for (i = 32; i < 127; i++)
string[string.length] = String.fromCharCode(i);
string = string.join('');
t0 = new Date();
for (i = 0; i < 10000; i++)
f(string);
return (new Date()) - t0;
}
document.write('O(n^2) = ',
timer(charRepeats), ';<br>O(n) = ',
timer(charUnique), ';<br>optimized O(n) = ',
timer(charUnique2), ';<br>more optimized O(n) = ',
timer(charCodeUnique), ';<br>regular expression way = ',
timer(regExpWay));
let myString = "Haammmzzzaaa";
myString = myString
.split("")
.filter((item, index, array) => array.indexOf(item) === index)
.join("");
console.log(myString); // "Hamza"
Another way of doing it using lodash
var _ = require("lodash");
var inputString = "HelLoo world!"
var checkRepeatition = function(inputString) {
let unique = _.uniq(inputString).join('');
if(inputString.length !== unique.length) {
return true; //duplicate characters present!
}
return false;
};
console.log(checkRepeatition(inputString.toLowerCase()));
const str = "afewreociwddwjej";
const repeatedChar=(str)=>{
const result = [];
const strArr = str.toLowerCase().split("").sort().join("").match(/(.)\1+/g);
if (strArr != null) {
strArr.forEach((elem) => {
result.push(elem[0]);
});
}
return result;
}
console.log(...repeatedChar(str));
You can also use the following code to find the repeated character in a string
//Finds character which are repeating in a string
var sample = "success";
function repeatFinder(str) {
let repeat="";
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str.charAt(i) == str.charAt(j) && repeat.indexOf(str.charAt(j)) == -1) {
repeat += str.charAt(i);
}
}
}
return repeat;
}
console.log(repeatFinder(sample)); //output: sc
const checkRepeats = (str: string) => {
const arr = str.split('')
const obj: any = {}
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) {
return true
}
obj[arr[i]] = true
}
return false
}
console.log(checkRepeats('abcdea'))
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = '
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = 'haiiiiiiiiii'
console.log(repeat(str))
'
console.log(repeat(str))
Cleanest way for me:
Convert the string to an array
Make a set from the array
Compare the length of the set and the array
Example function:
function checkDuplicates(str) {
const strArray = str.split('');
if (strArray.length !== new Set(strArray).size) {
return true;
}
return false;
}
You can use "Set object"!
The Set object lets you store unique values of any type, whether
primitive values or object references. It has some methods to add or to check if a property exist in the object.
Read more about Sets at MDN
Here how i use it:
function isIsogram(str){
let obj = new Set();
for(let i = 0; i < str.length; i++){
if(obj.has(str[i])){
return false
}else{
obj.add(str[i])
}
}
return true
}
isIsogram("Dermatoglyphics") // true
isIsogram("aba")// false

JavaScript stable sort issue

I've looked around for some help on this topic but was unable to find some help or guidance.
My problem is I am attempting to perform a sort on a series of values separated by an equals sign.
"Foo=Bar , Shenanigans=Fun, A=B ...etc"
My current sort works, but only if no value is the same. If I have some values like:
"Foo=Bar, A=Bar, Potato=Bar"
When the sort is complete they will all be "A=Bar"
My current sort looks like this, would someone be able to point me in the right direction?
$('#sortByValue').click(function() {
var textValueArray = document.getElementById('nameValuePairList');
textArray = new Array();
valueArray = new Array();
oldValues = new Array();
for (i = 0; i < textValueArray.length; i++) {
valueArray[i] = textValueArray.options[i].value;
textArray[i] = textValueArray.options[i].text;
oldValues[i] = textValueArray.options[i].value;
}
valueArray.sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
for (i = 0; i < textValueArray.length; i++) {
textValueArray.options[i].value = valueArray[i];
for (j = 0; j < textValueArray.length; j++) {
if (valueArray[i] == oldValues[j]) {
textValueArray.options[i].text = textArray[j];
j = textValueArray.length;
}
}
}
});
I know that my problem lies here: valueArray[i] == oldValues[j]
as when the data comes in valueArray = {Bar, Foo, Bar} while textArray = {Foo=Bar, A=Foo, Test=Bar}
However, I am unsure how to best resolve it.
Sort textArray directly, don't use valueArray since it will contain duplicates:
textArray.sort(function(a,b){
var aa = a.split('=')
var bb = b.split('=')
var a_key = aa[0].toLowerCase(), a_val = aa[1].toLowerCase();
var b_key = bb[0].toLowerCase(), b_val = bb[1].toLowerCase();
if (a_val == b_val) return a_key.localeCompare(b_key);
return a_val.localeCompare(b_val);
})
I would do something like this:
document.getElementById('sortByName').onclick = sortByName;
function sortByName(){
var myList = document.getElementById('list');
var values = [];
for (var i=0;i<myList.options.length;i++) {
values[i] = myList.options[i].text;
}
values.sort(function (a, b){
if(a !== "" && b !== ""){
return a.split('=')[0].localeCompare(b.split('=')[0]);
} else {
return 0;
}
});
clearList(myList);
fillList(myList, values);
}
function clearList(list) {
while (list.options.length > 0) {
list.options[0] = null;
}
}
function fillList(myList, values){
for (var i=0;i<values.length;i++) {
var option = document.createElement("option");
option.text = values[i];
myList.options[i] = option;
}
}
Take a look at this demo
The reasoning behind doing this at all will have you wondering why, in the future. I think you want something like this:
function inArray(v, a){
for(var i=0,l=a.length; i<l; i++){
if(a[i] === v){
return true;
}
}
return false;
}
function sortWeirdString(str){
var pairs = str.split(/\s?,\s?/), n = [], v = [], c = [], ci, idx = [], cl, nv = [], ra = [];
for(var i=0,l=pairs.length; i<l; i++){
var pair = pairs[i].split(/\s?=\s?/);
n.push(pair[0]); v.push(pair[1]);
}
c = n.concat().sort(); cl = c.length
for(var i=0; i<cl; i++){
var cv = c[i];
if(n.indexOf){
ci = n.indexOf(cv);
if(inArray(ci, idx)){
ci = n.indexOf(cv, ci+1);
}
idx.push(ci);
}
else{
for(var x=0; x<cl; x++){
if(n[x] === cv){
if(inArray(x, idx)){
continue;
}
idx.push(x);
}
}
}
}
for(var i=0,l=idx.length; i<l; i++){
ra.push(c[i]+'='+v[idx[i]]);
}
return ra.join(', ');
}
$('#sortByValue').click(function(){
console.log(sortWeirdString($('#nameValuePairList').val()));
}
Update 2019
The spec has changed and #Array.prototype.sort is now a stable sort.
The elements of this array are sorted. The sort must be stable (that
is, elements that compare equal must remain in their original order)
This is already implemented in V8

Removing empty strings from array

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to node.js.
for (var index in output)
{
if (opt.options.showEmpty != true)
{
var check = arrayIsEmpty(output[index]);
if ( check == true )
{
continue;
}
else
{
var array = removingEmptyString(output[index]);
console.log(index + "\t" + array);
//console.log(index+ "\t" + output[index]);
}
}
}
function removingEmptyString(array)
{
var newArray;
for( var i = 0; i < array.length; i++)
{
if(array[i] != "" || array[i] != null)
{
newArray[i] = array[i];
}
}
return newArray;
}
My result is tree,,, that i was previously getting before the code i wrote. now i get an error of
newArray[i] = array[i];
^
TypeError: Cannot set property '0' of undefined
at removingEmptyString (librarySeeker.js:130:18)
at result (librarySeeker.js:76:19)
at /async/lib/async.js:226:13
at async/lib/async.js:113:25
at async/lib/async.js:24:16
at async/lib/async.js:223:17
at /async/lib/async.js:510:34
at IncomingMessage.<anonymous> (pull.js:295:10)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
You could just use the .filter method in Array's prototype.
var pirate = ['a','1','',0];
function arr (value) {
return value.filter(function (item) {
return item !== '';
});
}
arr(pirate);
// <- ['a','1',0]
As an alternative, you might want to consider naming the callback to .filter
var pirate = ['a','1','',0];
function worthy (value) {
return value !== '';
}
pirate.filter(worthy);
// <- ['a','1',0]
In the spirit of learning, here is a working version of your solution:
function removingEmptyString(array) {
'use strict';
var newArray = []; // don't forget to initialize it
for( var i = 0, len = array.length; i < len; i += 1) {
if(typeof array[i] === 'string' && array[i].length > 0) {
// add the string to the end of the new array
newArray.push(array[i]);
}
}
return newArray;
}
The error is saying that newArray has not been initialised, so it cannot assign the 0 property to an undefined object.
You can improve your function to make it work:
function removingEmptyString(array){
var newArray = [];
for( var i = 0; i < array.length; i++){
// empty string and null are falsy values by default is js
if(array[i])
{
// use this if you want to keep "undefined" values in the newArray in place
// of the null ones in the original array
newArray[i] = array[i];
// otherwise just push the values in the new array
// newArray.push(array[i]);
}
}
return newArray;
}

Javascript for looping over array argument

I am trying to loop over an array argument and return the first n elements of the passed array without using standard javascript functions such as slice, concat, push, pop etc...
var n = 0;
var anyArray = Array;
var SR = {};
SR.first = function(anyArray,n){
var isArray = (Object.prototype.toString.apply(anyArray) === '[object Array]');
var specification = (typeof n === "number");
if(isArray && specification){
for(i = 0; i < n; i++){
return Array(anyArray[i]);
}
}
else if (isArray || !specification){
return anyArray[0];
}
}
I do not want to build the return array "anyArray" by using +=. So, how would I proceed to have it return some thing like this [1,2,3,4] when "SR.first([1,2,3,4,5,6,7], 4);" is called?
var newArr = Array.apply(null, anyArray); // new Array using original content
newArr.length = n; // truncate the length of the new Array
return newArr; // return it
One small edge case will be when anyArray has only one member, which is a number. You'll need to guard against that scenario.
I don't understand why you would not want to use Array operations like push and slice, but this would work:
if ( isArray && specification ) {
var result = [];
for ( var i = 0; i < n; i++ ) {
result[i] = anyArray[i];
}
return result;
}
else ...
If for some reason you really don't want to use native javascript functions, you can assign each element to your return array one by one.
var returnArray = [];
if(isArray && specification) {
for(i = 0; i < n; i++) {
returnArray[i] = anyArray[i];
}
}
return returnArray;
SR.first = function(anyArray,n){
var newArray = [];
for(i = 0; i < n; i++){
newArray[newArray.length] = anyArray[i];
}
return newArray;
}
First of all - global variable it's really bad practice! You don't need declare anyArray and n, becouse its a function arguments, and its declared on function call.
Second problem - that you can put number of elements bigger than array length - you must check this situation.
var SR = {};
SR.first = function(anyArray,n){
var isArray = (anyArray instanceof Array),
specification = (typeof n === 'number'),
tmp = new Array;
console.log(isArray, specification);
if(isArray && specification){
for(i = 0, l = anyArray.length; i < n && i < l; i++){
tmp[i] = anyArray[i];
}
return tmp;
} else if (isArray || !specification){
return anyArray[0];
}
}

Categories

Resources