Code Monkey home page Code Monkey logo

Comments (4)

skawaguchi avatar skawaguchi commented on August 24, 2024

You could also use Regex. But the exercise could also be to see how you would build this out in a more general way. I assumed that this was simple for the reason to see how would would modularize your code and go with a faster runtime, rather than to focus on how you would manipulate strings in a language that didn’t have these tools.

const charMap = {
   : %20
};

function replaceCharacter(str, targetChar, replacementChar) {
  const charRegex = new RegExp(targetChar, ‘gi’);

  return str.replace(charRegex, replacementChar);
}

function urlify(url) {
  let modifiedURL = url.trim();

  for(let charToReplace in charMap) {
    const replacementChar = charMap[charToReplace];

    modifiedURL = replaceCharacter(modifiedURL, charToReplace, replacementChar);
  }
  return modifiedURL;
}

Of course, if we are pretending there are no language features that trivialize the problem or we need to account for space (maybe the target device is embedded with insanely small resources), then we could do something along the lines of the existing code or the solution from the book.

Basically it seems your interviewer can make it as hard as they want to, so we should probably consider all cases.

from ctci-6th-edition-javascript.

liz-ete avatar liz-ete commented on August 24, 2024

Apparently there is an issue with this solution because when you run it the result it returns is missing some characters: Mr%20hohn%20ith%20.
Also without using any JS specific methods/properties you could still simplify this even more.

@skawaguchi @jarkin13 The thing that I don't like about using specific javascript methods/properties, specially if you can do without them, is that if your interviewer asks you about the Big O for your solution, unless you know what's happening behind curtains, you won't be able to provide an answer.

from ctci-6th-edition-javascript.

Elaniobro avatar Elaniobro commented on August 24, 2024

I came here to write, you could simply use regex, I do like your solution @jarkin13, I did not think to use trim();

const URLify = (string) => string.replace(/[ \t]+$/g, '').replace(/[ ]/g, '%20');

from ctci-6th-edition-javascript.

AmitJoki avatar AmitJoki commented on August 24, 2024

I think the following is a general solution and that which is more elegant.

var urlify = function(str, length) {
  var strArr = [];
  for(var i = 0; i < length; i++) {
     strArr[i] = str[i] == ' ' ? '%20' : str[i]; 
  }
  return strArr.join('');
};

I've submitted a PR for the same.

from ctci-6th-edition-javascript.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.