Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function firstWord(a) {
- // returns the first word in a given text.
- var output = '';
- var i;
- console.log('Input: [' + a + ']');
- for (i=0;i < a.length; i++){
- var t = a.charCodeAt(i)
- if (!i==0 && t!==32){
- console.log('Char: ' + a.charAt(i) + ' Char Code:' + t + ' i:' + i);
- if (t>=65 && t<= 90 || t==45 || t==32 || t>=97 && t<= 122) {
- if (t==32){ break; }
- output += a.charAt(i);
- }
- }
- }
- console.log('Output: ' + output)
- return output;
- }
- var assert = require('assert');
- if (!global.is_checking) {
- console.log('Example:')
- console.log(firstWord("Hello world"))
- // These "asserts" using for self-checking and not for auto-testing
- assert.equal(firstWord("Hello world"), "Hello")
- assert.equal(firstWord(" a word "), "a")
- assert.equal(firstWord("don't touch it"), "don't")
- assert.equal(firstWord("greetings, friends"), "greetings")
- assert.equal(firstWord("... and so on ..."), "and")
- assert.equal(firstWord("hi"), "hi")
- console.log("Coding complete? Click 'Check' to earn cool rewards!");
Advertisement
Add Comment
Please, Sign In to add comment