-1

I am optimizing a Java code to JS, but runs on Nashorn and do not own debug option. The input is val = "JPG ou PNG" and the output is "JPG ou PNG". Why does this happen? I need the output to be "jpg/png"

Function

function process(val) {
    var cleaned = val.replaceAll("[•×\\tª°▪º⊗ fi ²●˚~ĩ`ũ]", "").trim().toLowerCase();
    var out = [];
    if (cleaned.contains("ou")) {
        out = cleaned.split("ou");
    }
    else if (cleaned.contains("/")) {
        out = cleaned.split("/");
    }
    else {
        return cleaned;
    }
    for (var i = 0; i < out.length; i++) {
        out[i] = out[i].trim();
    }
    return join(out, "/");
}
brso05
  • 13,142
  • 2
  • 21
  • 40
Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • 2
    What are `replaceAll` and `contains`? [String.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/prototype) has no such methods – hindmost Apr 24 '15 at 14:23
  • these functions don't exist in JS? – Daniela Morais Apr 24 '15 at 14:24
  • 1
    Neither does `join`, when its used like that. Here is your code in javascript http://jsfiddle.net/s5wng5g8/1 – Pedro Estrada Apr 24 '15 at 14:24
  • @PedroEstrada `join` exist – Ahmad Ibrahim Apr 24 '15 at 14:25
  • you need to include `jquery` – ozil Apr 24 '15 at 14:25
  • According to [this](http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring) SO post there is no `contains` function. – npinti Apr 24 '15 at 14:25
  • @AhmadIbrahim its not used in that manner though, thats what i meant to say, the correct way is `array.join(separator)` – Pedro Estrada Apr 24 '15 at 14:26
  • 2
    @Daniela Marques de Morais Yes, they're missing in JS. You have to use [`replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) and [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) methods instead – hindmost Apr 24 '15 at 14:26
  • @ozil: jQuery has nothing to do with this. – Qantas 94 Heavy Apr 24 '15 at 14:40

2 Answers2

1

Three of your functions don't exist in javascript:

  1. replaceAll(searchValue, newValue) in javascript is replace(searchValue, newValue)

  2. contains(searchValue) in javascript is indexOf(searchValue) > -1

  3. join(array, separator) in javascript is array.join(separator)

JSFIDDLE DEMO

Pedro Estrada
  • 2,384
  • 2
  • 16
  • 24
1

Here's my solution:

function process(val) {
    var cleaned = val.replace("[•×\\tª°▪º⊗ fi ²●˚~ĩ`ũ]", "").trim().toLowerCase();
    var out = [];
    if (cleaned.indexOf("ou") >= 0) {
        out = cleaned.split("ou");
    }
    else if (cleaned.indexOf("/") >= 0) {
        out = cleaned.split("/");
    }
    else {
        return cleaned;
    }
    for (var i = 0; i < out.length; i++) {
        out[i] = out[i].trim();
    }
    return join(out, "/");
}

Your logic was right, but strings in Javascript don't have 'replaceAll' and 'contains', so I replaced them with 'replace' and 'indexOf(x) >= 0'.

Also, you mentioned you don't have the option to debug in your environment, yet the function you provided is pretty standalone. This means you could easily copy it into another environment to test it in isolation.

For example, I was able to wrap this code in a HTML file then open it in my web browser (I had to implement my own 'join').

<html>
<body>
<script>
function join(val, divider) {
    var out = "";
    for(var i = 0; i < val.length; i++) {
        if(out.length > 0) out += divider;
        out += val[i];
    }
    return out;
}

function process(val) {
    var cleaned = val.replace("[•×\\tª°▪º⊗ fi ²●˚~ĩ`ũ]", "").trim().toLowerCase();
    var out = [];
    if (cleaned.indexOf("ou") >= 0) {
        out = cleaned.split("ou");
    }
    else if (cleaned.indexOf("/") >= 0) {
        out = cleaned.split("/");
    }
    else {
        return cleaned;
    }
    for (var i = 0; i < out.length; i++) {
        out[i] = out[i].trim();
    }
    return join(out, "/");
}

var inval = "JPG ou PNG";
var outval = process(inval);
console.log(inval + " => " + outval);
</script>
</body>
</html>

I verified it works by opening up the console and seeing the output "JPG ou PNG => jpg/png".

Andrew Price
  • 269
  • 1
  • 3
  • 7