Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I log some tests for javascript?

Tags:

javascript

I was hoping to get your assistance with this "Is Unique" algorithm in Javascript.

var allUniqueChars = function(string) {

  // O(n^2) approach, no additional data structures used
  // for each character, check remaining characters for duplicates
  for (var i = 0; i < string.length; i++) {
    console.log(i);
    for (var j = i + 1; j < string.length; j++) {
      if (string[i] === string[j]) {
        return false; // if match, return false
      }
    }
  }
  return true; // if no match, return true
};

/* TESTS */
// log some tests here
allUniqueChars('er412344');

I am looking to log some tests, to see it display in the console. How do I call the function with unique strings to test it?

John

like image 886
JohnNgo Avatar asked Feb 19 '26 16:02

JohnNgo


1 Answers

You can always create an Array with your strings and test like:

var allUniqueChars = function(string) {

  // O(n^2) approach, no additional data structures used
  // for each character, check remaining characters for duplicates
  for (var i = 0; i < string.length; i++) {
    for (var j = i + 1; j < string.length; j++) {
      if (string[i] === string[j]) {
        return false; // if match, return false
      }
    }
  }
  return true; // if no match, return true
};

/* TESTS */
// log some tests here

[
'er412344',
'ghtu',
'1234',
'abba'
].forEach(v => console.log(allUniqueChars(v)));

MDN Array.prototype.foreach

like image 154
Roko C. Buljan Avatar answered Feb 21 '26 15:02

Roko C. Buljan