0

I am currently using JavaScript with HTML to create a number input, and once a number is input, an option from a select list tells whether to double the number or not. I feel I have done everything correctly, however, when I run my code and select yes to double, the output number is the number input and does not double. I am assuming my if statement is incorrect but I am not sure, I am new to JavaScript. Appreciate any feedback.

HTML

<body>
    <input id = 'number' name = '' value = '' class = ''>
    <select id = 'double' name = ''>
        <option value = 'Y'>YES</option>
        <option value = 'N'>NO</option>
    </select>
    <button id = 'go' class = ''>GO</button>
    <input id = 'result' name = '' value = '' class = ''>
    <script src = 'js/javascript 03.js'></script>
</body>

JavaScript

document.getElementById('go').onclick = function () {
    var number = document.getElementById('number').value;
    var double = document.getElementById('double').value;
    number = parseFloat(number);

    if (double == 'Y') {
        number * 2;
    };

    document.getElementById('result').value = number;
};
5
  • 5
    number * 2 does indeed double the number... but with no = in that line it doesn't get assigned to anything. Commented Oct 6, 2017 at 16:41
  • 2
    You need to reassign the variable number, use number = number * 2; Commented Oct 6, 2017 at 16:41
  • @NiettheDarkAbsol but if i assign number = number*2 that defeats the purpose of choosing yes to double and no to keep the number the same. it just doubles it every time Commented Oct 6, 2017 at 16:49
  • Spaces between attributes, = and values is invalid html, i'm not quite sure that it will even render properly. Commented Oct 6, 2017 at 18:22
  • 1
    @Astonishing Nooooo... because you're doing that inside the if(double == 'Y') check... Commented Oct 6, 2017 at 18:27

3 Answers 3

1

number * 2; does not store the value anywhere. You should use one of the 2 cases here:

number *= 2;
number = number * 2;
Sign up to request clarification or add additional context in comments.

1 Comment

but if i assign number = number*2 that defeats the purpose of choosing yes to double and no to keep the number the same. it just doubles it every time
0

As mentioned in the comments to your question, doing just number * 2 will evaluate to a value. But that value is not stored anywhere, so you need to put it back in its own variable:

number = number * 2;

Comments

0
    int colCount=5;

    File fpath = new File("C:\\Users\\man.xls");
    FileInputStream file = new FileInputStream(fpath);

    if (fpath.exists()) {
        HSSFWorkbook wb = new HSSFWorkbook(file);
        int sCount =wb.getActiveSheetIndex();
        Sheet sh = wb.getSheetAt(sCount);

        int rwNum = sh.getLastRowNum();
        sh.createRow(rwNum+1);
        int rwNuafterr = sh.getLastRowNum();

        Row rw = sh.getRow(rwNuafterr);
        for (int j = 0; j <= colCount; j++) {
            rw.createCell(j);
            rw.getCell(j).setCellValue("char");
        }
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\man.xls");
        wb.write(outputStream);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.