Bonejob

Untitled

May 18th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function firstWord(a) {
  2.     // returns the first word in a given text.
  3.     var output = '';
  4.     var i;
  5.     console.log('Input: [' + a + ']');
  6.     for (i=0;i < a.length; i++){
  7.         var t = a.charCodeAt(i)
  8.         if (!i==0 && t!==32){
  9.             console.log('Char: ' + a.charAt(i) + ' Char Code:' + t + ' i:' + i);
  10.             if (t>=65 && t<= 90 || t==45 || t==32 || t>=97 && t<= 122) {
  11.                 if (t==32){ break; }
  12.                 output += a.charAt(i);
  13.             }
  14.         }
  15.     }
  16.     console.log('Output: ' + output)
  17.     return output;
  18. }
  19.  
  20. var assert = require('assert');
  21. if (!global.is_checking) {
  22.     console.log('Example:')
  23.     console.log(firstWord("Hello world"))
  24.  
  25.     // These "asserts" using for self-checking and not for auto-testing
  26.     assert.equal(firstWord("Hello world"), "Hello")
  27.     assert.equal(firstWord(" a word "), "a")
  28.     assert.equal(firstWord("don't touch it"), "don't")
  29.     assert.equal(firstWord("greetings, friends"), "greetings")
  30.     assert.equal(firstWord("... and so on ..."), "and")
  31.     assert.equal(firstWord("hi"), "hi")
  32.     console.log("Coding complete? Click 'Check' to earn cool rewards!");
Advertisement
Add Comment
Please, Sign In to add comment