Jump to content

Using negative numbers in a substring


mrkinney

Recommended Posts

I am teaching a course on the ACSL competition to middle school students and my programming skills are little rusty. In the next competition we have a programming challenge where they need to create a program that you can input a string and several substring parameters. The problem is that they need to be able to input a negative number and have it produce the desired result. For example:

 

with the string "Hello world!"

input 1: 0, -1 output 1: Hello world

input 2: 0,-5 output 2: Hello w

input 3: -4,0 output 3: rld!

 

I am at a little bit of a loss as how to go about getting the desired output.

Link to comment
Share on other sites

All you need to do is look at substring(i,j) and translate the values if they are negative.

 

http://www.w3schools.com/jsref/jsref_substring.asp

 

Perhaps something like...

String.prototype.supersubstring = function(i,j){

var len = this.length;

while(i<0){

i = len + i;

}

while(j<0){

j = len + j;

}

return this.substring(i,j);

};

--edited jan 10, 1:55pm cdt

Link to comment
Share on other sites

I think you meant to use slice() rather than substring(), the values in your example are not valid (negative values not allowed in substring()), only with manipulation to produce valid positive values will it then produce correct result.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <script type="text/javascript">

            window.onload = function() {

                a = document.getElementById('submit');

                a.onclick = function() {
                    process_this_string();
                }

            }

            function process_this_string() {

                b = document.getElementById('myform');

                var param_count = 0;


                for (i = 0; i < b.elements.length; i++)
                {
                    var string_value = b.elements[0].value;

                    if (b.elements[i].name !== "string" && b.elements[i].name !== "submit_but")
                    {
                        param_count++;
                        var split_params = b.elements[i].value.split(",");

                        if (split_params[0] < 0) {
                            document.getElementById('result' + param_count).innerHTML = string_value.slice(split_params[0]); //slice
                            document.getElementById('resultb' + param_count).innerHTML = string_value.substring(string_value.length + parseInt(split_params[0]), string_value.length); //substring
                        }
                        else
                        {
                            document.getElementById('result' + param_count).innerHTML = string_value.slice(split_params[0], split_params[1]); //slice
                        }

                        if (split_params[1] < 0) {

                            document.getElementById('resultb' + param_count).innerHTML = string_value.substring(split_params[0], string_value.length + parseInt(split_params[1]));//substring
                        }
                    }
                }

            }


        </script>
        <style>




        </style>
    </head>
    <body>
        <form id="myform">
            <div><label>String:   <input type="text" value="Hello world!"  name="string" required></label></div>
            <div><label>Input 1 parameters: <input type="text" value="" name="params1"  required> </label></div>
            <div><label>Input 2 parameters:  <input type="text" value="" name="params2" required ></label></div>
            <div><label>Input 3 parameters:  <input type="text" value="" name="params3"  required></label></div>

            <input type="button" value="Submit" name="submit_but" id="submit">
        </form>
        <h3>slice()</h3>
        <div id="result1"></div>
        <div id="result2"></div>
        <div id="result3"></div>

        <h3>substring()</h3>
        <div id="resultb1"></div>
        <div id="resultb2"></div>
        <div id="resultb3"></div>

    </body>
</html>
Link to comment
Share on other sites

As you can see, your problem description wasn't very clear to those of us who responded. And you don't really write a "program" in Javascript, you write a collection of functions and event handlers, which interact with a particular set of page elements. And you did not really define the desired user interface or provide enough test cases.

 

We do have a Javascript tutorial and reference that you might look at as a refresher...

 

http://www.w3schools.com/js/default.asp

 

http://www.w3schools.com/jsref/default.asp

Link to comment
Share on other sites

What you write in Javascript is a program. It's a sequence of instructions that perform a task.

 

Do you think so? Of course it is just semantics, but a webpage is a collection of routines. Is it one program or many?

Link to comment
Share on other sites

If there's Javascript running on the page then there's at least one program, since Javascript is, by all definition, a programming language.

 

I have built applications in Javascript that could easily be rewritten in Java or C++. Why would the same set of instructions be considered a program in one programming language but not another?

 

The line between program and not-program blurs when the Javascript is just used for minor alterations in behavior for some elements on the page.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...