A simple js interview question

Source: Internet
Author: User

I saw some people asking questions in the js group, so I wrote a few questions with my hands-on mentality. Although the questions were not too difficult, they tested people's programming thinking. After reading others' answers, I found myself stupid. Put the code on. 1. If the result of adding 1 to the last element of an array is greater than 10, add 1 to the previous element and set the current element value to 0. The same applies to the previous element, for example, var a = new Array (); a [1] + 1; a = []; this question seems very simple, if the last element is equal to 10, add 1 to the money element and set the current element to 0. However, when all elements in the array are 9, add an element to the array. The answer is as follows: copy the code $ (function () {add () ;}) function add () {var a = [9, 4]; var B = a; var sum; B [B. length-1] = (B [B. length-1]) + 8; if (B [B. length-1]> = 10) {B [B. length-1] = B [B. length-1]-10; var I = B. length-2; while (I>-2) {if (I! =-1) {sum = B [I] + 1; if (sum> = 10) {B [I] = sum-10 ;} else {B [I] = sum; break;} I --;} else {a = [1];. push (B); I -- ;}}else {a = B;} alert (a) ;}copy the code and read the code written by others, I found myself complicated. I wrote a bunch of code. If I use the js unshift () function, it would be better to add one or more elements to the array header and return a new number length. Khan, I don't know much about my js. However, the essence of this writing is the same. I personally think the most correct writing method should be like this: copy the code // convert the integer and convert it into an array $. (function () {var num = [9, 9, 9, 9], intA = parseInt (num. join (","). replace (/,/gi, ""); intA ++; var newStr = intA. toString (), newArr = []; for (var I = 0, l = newStr. length; I <l; I ++) {newArr. push (parseInt (newStr [I]);} alert (newArr) ;}); Copying code is a matter of simulating decimal addition with arrays, therefore, converting to a number is the most direct and simple. At the same time, I also found a problem. Why is the gap between myself and others so big? There is also a query for the number of occurrences of each character in a string. For example, a s d f appears several times in 'assddfsds '. It was written but not saved, if you are not interested, you can check it yourself. Note that only JavaScript syntax is supported;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.